Java存取CLOB方法

import java.io.IOException;
import oracle.sql.CLOB;
import java.sql.*;
import java.io.Writer;

public void insertClob(){
Connection conn = null;
String sql = null;
sql = "insert into doc_info (keyid,bt,nr) values (?,?,empty_clob())";
ResultSet rs = null;
PreparedStatement pstmt = null;
try {
   Class.forName("oracle.jdbc.driver.OracleDriver"); 
   conn=DriverManager.getConnection("jdbc:oracle:thin:@10.68.1.1:1521:test","test","test"); 
     conn.setAutoCommit(false);    
     pstmt = conn.prepareStatement(sql);
     pstmt.setString(1, "唯一关键字");
     pstmt.setString(2, "标题");
     pstmt.executeUpdate();
     pstmt = null;
     sql = "select nr from doc_info where keyid= 唯一关键字 for update";
     pstmt = conn.prepareStatement(sql);
     rs = pstmt.executeQuery();
     CLOB clob = null;
     if(rs.next()){
      clob = (CLOB)rs.getClob(1);
     }
     Writer wr = clob.getCharacterOutputStream();   // 此方法已不推荐使用,哪位朋友知道被替换的方法,请告知,谢谢
     wr.write("CLOB内容");
     wr.flush();
     wr.close();
     rs.close();
     conn.commit();
} catch (SQLException e) {
     e.printStackTrace();
} catch (IOException e) {
     e.printStackTrace();
}finally{
     try {
      rs.close();
      pstmt.close();
      conn.close();
} catch (SQLException e) {
      e.printStackTrace();
}
}
}

你可能感兴趣的:(java,oracle,sql,jdbc)