简体   繁体   English

getBytes()使用ResultSet但不使用CachedRowSet

[英]getBytes() working with ResultSet but not CachedRowSet

I have a Derby SQL database in which I have a table containing a blob field that needs to contain a serialized object. 我有一个Derby SQL数据库,其中有一个表,其中包含一个需要包含序列化对象的blob字段。 I access it through JDBC. 我通过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. 问题是,当我使用ResultSet反序列化对象时,一切正常,但是如果使用CachedRowSet,则会出现“数据类型不匹配”异常。

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 implementation com.sun.rowset.CachedRowSetImpl ) stores a copy of the data in the ResultSet within the cached rowset. CachedRowSet (假设参考实现com.sun.rowset.CachedRowSetImpl )将数据副本存储在缓存行集中的ResultSet中。 It does this using the getObject method of the result set provided to populate . 它使用提供给populate的结果集的getObject方法执行此操作。

As you indicate that the column is a blob, I assume that getObject will return a Blob and the column type in metadata is BLOB and not a byte array (type VARBINARY or LONGVARBINARY ). 如您所指出的那样,列是一个Blob,我假设getObject将返回一个Blob并且元数据中的列类型是BLOB而不是字节数组(类型VARBINARYLONGVARBINARY )。 Therefor the cached rowset will only allow you to retrieve the column as a Blob , and not as a byte[] even if the original result set supports that. 因此,即使原始结果集支持,缓存的行集也仅允许您以Blob而不是byte[]检索列。

The JDBC 4.2 specification (Appendix B.6) describes which methods are supported for which types, and for a BLOB , only getBlob (and getObject ) are to be supported. JDBC 4.2规范(附录B.6)描述了哪些类型支持哪些方法,而BLOB仅支持getBlob (和getObject )。 However contrary to requirements in the specification a lot of drivers are more lenient and also support getBytes and getBinaryStream for BLOB . 但是,与规范中的要求相反,许多驱动程序都比较宽松,并且还支持BLOB getBytesgetBinaryStream In this regard, the CachedRowSetImpl is more strict in its interpretation of JDBC. 在这方面, CachedRowSetImpl对JDBC的解释更为严格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM