代码应该是没错的啦:
Connection conn = null;
String sql = null;
try{
sql = "SELECT rpt_template FROM RMS_REPORT_DEFINE " +
"WHERE report_id='"+reportId+"' FOR UPDATE";
conn = DBTools.getConnection();
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
ResultSet rst = stmt.executeQuery(sql);
if(rst.next()){
Blob blob = rst.getBlob(1);
oracle.sql.BLOB bstr = (oracle.sql.BLOB)blob;
BufferedOutputStream out = new BufferedOutputStream(bstr.getBinaryOutputStream());
ByteArrayInputStream in = new ByteArrayInputStream(stream.toByteArray());
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
sql = "UPDATE RMS_REPORT_DEFINE SET template_name='"+fileName
+"',modi_user_id='" + user
+ "',modi_date=SYSDATE WHERE report_id='"+reportId+"'";
Statement st = conn.createStatement();
st.executeUpdate(sql);
conn.commit();
}
}catch(UserException ue){
try{
conn.rollback();
}catch(Exception e){
e.printStackTrace();
}
throw ue;
}catch(Exception ex){
try{
conn.rollback();
}catch(Exception e){
e.printStackTrace();
}
ex.printStackTrace();
throw new UserException(new ExpMessage());
}finally{
try{
if(conn!=null){
conn.setAutoCommit(true);
conn.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
可供参考以下如何写入Oracle的BLOB字段。
找了大半天原因才发现,因为tomcat部署的原因会引起oracle.sql.BLOB bstr = (oracle.sql.BLOB)blob;这行报ClassCastException异常。我在%tomcat_home%/common/lib下和%web包%/WEB-INF/lib下都放置了oraclejdbc.jar的程序包,出现这样的异常错误,我只要将/WEB-INF/lib底下的oraclejdbc.jar删除则没问题了。不知道为什么这样?
同样的情况在resin下倒是没问题。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/67736/viewspace-799719/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/67736/viewspace-799719/