简体   繁体   English

Spring JDBC-将对象转换为Blob时发生ClassCastException

[英]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与Ob​​ject相同。

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.

相关问题 强制转换为同一对象时发生ClassCastException - ClassCastException when casting to same object 强制转换为对象数组时发生ClassCastException - ClassCastException when casting to object array 将对象Array转换为Long数组时出现ClassCastException - ClassCastException when casting object Array to Long array 将 Object 转换为 String 时出现 ClassCastException,但将 Object 转换为自定义类时没有 ClassCastException - ClassCastException when casting Object to String, but no ClassCastException when casting Object to Custom class 投射 TargetDataLine 时出现 ClassCastException - ClassCastException when casting TargetDataLine 将 Class.forName 对象转换为此对象实现的接口时出现 ClassCastException - ClassCastException when casting Class.forName object to interface implemented by this object 将基础类对象转换为扩展基础类的类时的ClassCastException - ClassCastException when casting a base class object to an class that extends base class Java中将Object []数组转换为泛型类型数组时发生ClassCastException - ClassCastException when casting Object[] array to generic type array in Java 在Spring Redis会话中保存对象时发生ClassCastException - ClassCastException when saving object in Spring redis session 将数组强制转换为readObject时发生ClassCastException - ClassCastException when casting array to readObject
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM