1. 必須保證在一個事務內處理:JDBC connection autocommit conn.setAutoCommit(false);
2. 插入記錄用empty_clob()函數帶入空CLOB字段:Insert into table with CLOB column as empty_clob();
3. 用select把空CLOB對象查出,注意必須帶for update子句來告知數據庫接下來要修改該記錄,否則SQL語句會返回錯誤告訴你沒有for update子句:Select clob column with “for update” option like “select clob_field from clob_table where rowid=1 for update”;
4. 將返回的CLOB字段對象由JDK的Clob轉換成Oracle 庫的CLOB對象:Turn the return from java.sql.Clob to Oracle.sql.CLOB:
Clob clob = (Clob)rs.getClob("clob_field");
oracle.sql.CLOB tmpclob = (oracle.sql.CLOB)clob;
5. 用字符串填充該CLOB對象:
BufferedWriter bw = new BufferedWriter(tmpclob.getCharacterOutputStream());
bw.write(clobClValueArray.get(i).toString());
bw.flush();
bw.close();
6. 用結構化語句對象PreparedStatement實現DML操作:
PreparedStatement pstmt1 = conn.prepareStatement(“update clob_table set clob_field=? Where rowid=1”);
pstmt1.setClob(1, tmpclob);
pstmt1.execute();
7. 把事務提交實現CLOB字段操作。Commit the update: conn.commit();
8.讀取CLOB內容也很簡單:
PreparedStatement pstmt = conn.prepareStatement("select clob_field from clob_table where rowid=1");
ResultSet rs = pstmt.executeQuery();
Clob clob = (Clob)rs.getClob("clob_field");
String str;
if(null != clob){
str = clob.getSubString((long)1, clob.length());
}