在插入clob字段时,有时会出记录插入,但是CLOB字段的内容提要没有被插入. 代码片断如下所示 , 请帮忙分析下原因
PreparedStatement pstmt = null;
ResultSet rs = null;
try{
//step1: 插入一条空数据
pstmt = conn.prepareStatement("insert into T_BO (SEQ, C_CLOG) VALUES (0,EMPTY_CLOB() ) " ;
if (pstmt.executeUpdate() == 1) {
log("Successfully inserted a clob record");
}
//step2: 插入CLOB字段
pstmt=conn.prepareStatement("select bo from T_BO where seq=0 for update nowait");
rs=pstmt.executeQuery();
String clobData = "this is a test string.";
while(rs!=null&&rs.next()){
oracle.sql.CLOB clob= (oracle.sql.CLOB)rs.getClob(1);
Writer outstream = clob.getCharacterOutputStream(); 
outstream.write(clobData,0,clobData.length());
outstream.close();
break;
}
conn.commit();
} catch (SQLException e) {
printErr(e);
} catch (Exception ex) {
printErr(ex);
} finally {
close_conn(rs, pstmt);
}

解决方案 »

  1.   

    插入大字段,分两步,先插入记录,其CLOB字段为空,第二步再写入CLOB字段
    问题描述:有时会出现CLOB字段没有插入的情况.
     
      

  2.   

    while循环部分改为下面的代码试试
    if(rs!=null){
          while(rs.next()){
            oracle.sql.CLOB clob= (oracle.sql.CLOB)rs.getClob(1);
            Writer outstream = clob.getCharacterOutputStream(); 
            outstream.write(clobData.toCharArray(),0,clobData.length());
            outstream.flush();
            outstream.close();
            //break;//为啥这样写,如果这样写,何必用while呢
        }
       }