以公告Notice的公告内容noticeContent为例说明:
Notice表notice_content字段为clob类型;
Notice类的noticeContent属性为String;
Notice_hbm_xml映射文件为text类型:
<property name="noticeContent" type="text">
<column name="NOTICE_CONTENT" />
</property>
LOB数据不能象其它类型数据一样直接插入(INSERT)。 插入前必须先插入一个空的LOB对象,CLOB类型的空对象为EMPTY_CLOB (),BLOB类型的空对象为EMPTY_BLOB()。
之后通过SELECT命令查询得到先前插入的记录并锁定,继而将空对象修改为所要插入的LOB对象。
在插入到更新之间一定要将自动提交设为false,否则,再次查找时就不能正确更新,查找时一定要用select XXX from table where ... for update
如果不加for update会报:“row containing the LOB value is not locked”;
如果在插入前没有将自动提交设为false会报 “fetch out of sequence”。
实例如下:
StringBuffer sqlstr = new StringBuffer();
("INSERT INTO notice t (t.notice_id,t.notice_type,t.notice_title," +
"t.notice_status,,t.eff_date,t.exp_date,,t.create_time," +
"t.notice_content) VALUES (");
(+() + ",");
("'" + () + "',");
("'" + () + "',");
("'" + () + "',");
("'" + () + "',");
("sysdate,");
("to_date('" + theForm.getEndDate2() + " 23:59:59','yyyy-MM-dd HH24:mi:ss'),");
("'" + () + "',");
("sysdate,");
("empty_clob()");//插入空值
(")");
String sql = ();
(driverClassName);
Connection con = (url, username, password);
(false);//设置不自动提交
PreparedStatement pstmt = (sql);
String content = ().replace('\'', '\"');
try {
();
();//插入
pstmt = ("select notice_content from notice where notice_ for update");//查找刚刚插入的那条记录 for update
ResultSet res = ();
CLOB clob = null;
while(()){
clob = ()(1);
(1, content);//content为前台提交过来的公告内容,大数据量
}
pstmt = ("update notice set notice_content = ? where notice_id=?");//修改notice_content字段的内容
(1, clob);
(2, ());
();
();
if (nps != null && () > 0) {
(nps);
}
} catch (Exception e) {
("1");
(false);
("发布公告失败!");
(());
(());
(e, "发布公告失败");
return info;
} finally {
if(pstmt!=null) {
();
}
if(con!=null) {
();
}
}
对于clob的修改,可在修改该表的其他字段信息时同时将clob字段修改为EMPTY_CLOB (),然后才对clob字段单独修改,方法与上相同。
存入clob类型的文字一般很多,页面不会直接使用textarea,可以使用开源的CKEditor文字编辑器代替,使用方法很简单很实用,详见:CKEditor的使用示例