-
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());
}