-
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());
}
本文转自 dannyy1026 51CTO博客,原文链接:
http://blog.51cto.com/dannyyuan/550861