日期:2014-05-18  浏览次数:21116 次

Tomcat下用BLOB入库出现的问题~~急
我是通过JNDI获得数据库连接,现在需要把前台上传的一些图片和音频视频文件存储到Oracle数据库中,但出现错误!

Context   context   =   new   InitialContext();
DataSource   dataSource   =   (DataSource)   ctx.lookup( "java:comp/env/ "+jdbc/gab);
Connection   con   =   dataSource.getConnection();
Statement     st   =   con.createStatement();
con.setAutoCommit(false);
String   sql   =   "insert   into   olympics_opus_info   values( "+id+ ", ' "+opus.getUp_name()+ " ', "   +
"   ' "+opus.getUp_phone()+ " ', ' "+opus.getUp_address()+ " ', ' "+opus.getUp_postcode()+ " ',sysdate, ' "+opus.getOpus_type()+ " ', "   +
"   '0 ', ' ',empty_blob(),0, ' ', ' "+opus.getOpus_title()+ " ', ' "+opus.getOpus_describle()+ " ', ' "+opus.getUp_code()+ " ', "   +
"   ' "+opus.getUp_unit()+ " ', ' "+opus.getUp_mail()+ " ', ' "+opus.getUp_fax()+ " ', ' "+opus.getCollect_creation()+ " ', "   +
"   ' "+opus.getOrganization()+ " ', ' "+opus.getOpus_name()+ " '   ) ";
try{
  st.executeUpdate(sql);
  sql   =   "select   opus   from   olympics_opus_info   where   id   = "+id+ "   for   update ";
rs   =     st.executeQuery(sql);
if   (rs.next())   {
                      BLOB   blob   =   (BLOB)   rs.getBlob(1);
      OutputStream   outStream     =   blob.getBinaryOutputStream();
                      outStream.write(c);
                      outStream.flush();
                      outStream.close();
          }
con.commit();
}   catch   (SQLException   e)   {
try   {
con.rollback();
}   catch   (SQLException   e1)   {
}
}finally{
if(rs   !=   null){
try   {
rs.close();
rs   =   null;
con.setAutoCommit(true);
con.close();
}   catch   (SQLException   e)   {
e.printStackTrace();
}
}
}

}

在BLOB   blob   =   (BLOB)   rs.getBlob(1);这行出现ClassCast   异常,我用的是oracle.sql.BLOB,知道用错了,但不知道怎么改,请知道的人给些指点~~~还有在WebSphere环境下,此错误又该怎么改?



------解决方案--------------------
??怎么发了两条了??

BLOB blob=(BLOB)((OracleResultSet)rs).getBlob( "1 ");
需要转化一下 ···
其中 OracleResultSet 是 import oracle.jdbc.OracleResultSet; 导入的 ··
还有后面的(BLOB)强制转换 可能不要 ···
你试试看···