java insert Blob as ByteArrayOutputStream get ClassCastException [英] java insert Blob as ByteArrayOutputStream get ClassCastException
本文介绍了java insert Blob as ByteArrayOutputStream get ClassCastException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须将表示为ByteArrayOutputStream的PDF文件保存到表格的Blob SQL字段中,以下是我的代码:
I've to save a pdf file represented as a ByteArrayOutputStream into a Blob SQL field of a table, here's my code:
public boolean savePDF(int version, ByteArrayOutputStream baos) throws Exception{
boolean completed = false;
ConnectionManager conn = new ConnectionManager();
try {
PreparedStatement statement = conn.getConnection().prepareStatement(INSERT_PDF);
statement.setLong(1, version);
statement.setBlob(2, (Blob)baos);
statement.execute();
conn.commit();
completed = true;
} catch (SQLException e) {
conn.rollbackQuietly();
e.printStackTrace();
throw e;
} catch (ClassNotFoundException e) {
e.printStackTrace();
}finally{
conn.close();
}
return completed;
}
但我得到一个java.lang.ClassCastException:
But I get a java.lang.ClassCastException:
java.io.ByteArrayOutputStream cannot be cast to java.sql.Blob
如何管理?感谢
推荐答案
有一个 setBlob
c> InputStream ,因此
There is a setBlob
that takes an InputStream
, so
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
statement.setBlob(2, bais);
这篇关于java insert Blob as ByteArrayOutputStream get ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文