操作Oracle的clob型別字段

2021-09-25 02:23:14 字數 1723 閱讀 5054

引用:

1.插入和更新

插入一條資料,注意clob欄位,需要先插入乙個空的clob型別 empty_clob(),然後再單獨更新clob欄位

在插入到更新之間一定要將自動提交設為false,否則,再次查詢時就不能正確更新,查詢時一定要用select *** from table where ... for update   

如果不加for   update會報:「row containing the lob value is not locked」;

如果在插入前沒有將自動提交設為false會報  「fetch out of sequence」。

string sql = "insert into user_courseware(user_id,courseware_id,progress,report ,id)values( ?,?,?,empty_clob(),  user_courseware_sq.nextval  )";

conn.setautocommit(false);//設定不自動提交  

preparedstatement pstmt = conn.preparestatement(sql);

pstmt.setint(1, userid);

pstmt.setint(2, courseware_id);

pstmt.setint(3, progress);

pstmt.executeupdate();

conn.commit();

pstmt = null;

resultset rs = null;

clob clob = null;

string sql1 = "select report from user_courseware where user_id=? and courseware_id=? for update";

pstmt = conn.preparestatement(sql1);

pstmt.setint(1, userid);

pstmt.setint(2, courseware_id);

rs = pstmt.executequery();

if (rs.next())

writer writer = clob.getcharacteroutputstream();

writer.write(courseclob);

writer.flush();

writer.close();

conn.commit();

2.查詢

clob clob = rs.getclob("module_info");

方法一:

byte info = clob!=null?clob.getsubstring((long)1,(int)clob.length()).getbytes():null;

方法二:

string line = "";

string value = "";

reader reader = clob.getcharacterstream();

bufferedreader br = new bufferedreader(reader);

while ((line = br.readline()) != null)

system.out.println(value);

byte info = value.getbytes();

java操作Oracle的CLOB型別

今天同事問我是否用過oracle中的clob型別,說實話 沒聽過。oracle 中sql 語句中的兩個單引號之間的字元數不能大於 4000 的限制。data data 在sql語句之間,當data的值大於 4000 個位元組時 就會報錯。oracle 往有clob型別的表中插資料 1.其他字段照常插...

oracle中clob型別的使用

oracle資料庫當需要存入大資料量 大於4000 時,varchar2不夠用,可以使用clob,本文描述clob怎麼和hibernate一起使用。以公告notice的公告內容noticecontent為例說明 notice表notice content欄位為clob型別 notice類的notic...

Oracle 游標 以及CLOB的簡單操作

sample declare cursor mycursor is select from auto event where event id between 54652 and 54681 begin for myoncursor in mycursor loop if myoncursor.st...