I have a Derby SQL database in which I have a table containing a blob field that needs to contain a serialized object. I access it through JDBC. The problem is that when I de-serialize the object using a ResultSet all works fine, but if I use a CachedRowSet I get a "Data Type Mismatch" Exception.
Here is the bit of code that works:
ResultSet rs = stmt.executeQuery();
rs.next();
byte[] buf = rs.getBytes("albero");
Here is the alternative bit tha
CachedRowSet crs = null;
ResultSet rs = stmt.executeQuery();
crs = RowSetProvider.newFactory().createCachedRowSet();
crs.populate(rs);
crs.next();
byte[] buf = crs.getBytes("albero");
Could anyone help me understand why this different behaviour? Thanks
The
CachedRowSet(assuming the reference implementationcom.sun.rowset.CachedRowSetImpl) stores a copy of the data in theResultSetwithin the cached rowset. It does this using thegetObjectmethod of the result set provided topopulate.As you indicate that the column is a blob, I assume that
getObjectwill return aBloband the column type in metadata isBLOBand not a byte array (typeVARBINARYorLONGVARBINARY). Therefor the cached rowset will only allow you to retrieve the column as aBlob, and not as abyte[]even if the original result set supports that.The JDBC 4.2 specification (Appendix B.6) describes which methods are supported for which types, and for a
BLOB, onlygetBlob(andgetObject) are to be supported. However contrary to requirements in the specification a lot of drivers are more lenient and also supportgetBytesandgetBinaryStreamforBLOB. In this regard, theCachedRowSetImplis more strict in its interpretation of JDBC.