[英]Spring JDBC - ClassCastException when casting Object to Blob
I have a table with few fields out of which 2 are of Varchar and Blob type. 我有一个表,其中有几个字段,其中2个是Varchar和Blob类型。 When I'm retrieving them using queryForMap, I'm getting a map instance with two keys(name of the column).
当我使用queryForMap检索它们时,我得到了一个带有两个键(列名)的地图实例。 I'm able to cast varchar to String simply but getting ClassCast Exception doing the same with Object.
我能够简单地将varchar转换为String,但是让ClassCast Exception与Object相同。
file = new File(System.currentTimeMillis() + (String) map.get("SAMPLE_DOC_FILE_NAME"));
blob = (Blob) map.get("SAMPLE_DOC");
My DAO layer method is: 我的DAO图层方法是:
public Map<String, Object> getSampleDoc1(String docName) throws SQLException {
String query = "select form.sample_doc_file_name as SAMPLE_DOC_FILE_NAME, form.sample_document as SAMPLE_DOC from forms form where form.document_name=?";
return localJdbcTemplateObject.queryForMap(query, docName);
}
Exception - java.lang.ClassCastException: [B cannot be cast to java.sql.Blob
异常
java.lang.ClassCastException: [B cannot be cast to java.sql.Blob
What can I do to get back this object as Blob? 我该怎么做才能将该对象恢复为Blob?
A Blob
is just a wrapper for a (possibly large) byte[]
. Blob
只是(可能是很大的) byte[]
的包装。 What you're getting back from Spring here is the interesting raw data, the byte[]
(or B[
in the exception's notation). 从Spring返回的是有趣的原始数据,
byte[]
(或B[
例外表示法)。 So, just use that instead, it'll be much easier to work with: 因此,只需使用它,使用起来就会容易得多:
byte[] blob = (byte[]) map.get("SAMPLE_DOC");
Please check what class it has as this: 请检查其具有的此类:
System.out.println(map.get("SAMPLE_DOC").getClass().getName());
then cast to this type, then you can use the API of this type to do something with it. 然后强制转换为该类型,则可以使用此类型的API对其进行处理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.