分享

oracle中clob类型的使用

 老星果子 2018-05-24
oracle数据库当需要存入大数据量(大于4000)时,varchar2不够用,可以使用clob,本文描述clob怎么和Hibernate一起使用。

以公告Notice的公告内容noticeContent为例说明:

Notice表notice_content字段为clob类型;

Notice类的noticeContent属性为String;

Notice_hbm_xml映射文件为text类型:

  1. <property name="noticeContent" type="text">  
  2.    <column name="NOTICE_CONTENT" />  
  3. </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”。


实例如下:

 

  1. StringBuffer sqlstr = new StringBuffer();  
  2.         sqlstr.append("INSERT INTO notice t (t.notice_id,t.notice_type,t.notice_title," +  
  3.                 "t.notice_status,t.sender,t.eff_date,t.exp_date,t.creater,t.create_time," +  
  4.                 "t.notice_content) VALUES (");  
  5.         sqlstr.append(+notice.getNoticeId() + ",");  
  6.         sqlstr.append("'" + notice.getNoticeType() + "',");  
  7.         sqlstr.append("'" + notice.getNoticeTitle() + "',");  
  8.         sqlstr.append("'" + notice.getNoticeStatus() + "',");  
  9.         sqlstr.append("'" + notice.getSender() + "',");  
  10.         sqlstr.append("sysdate,");  
  11.         sqlstr.append("to_date('" + theForm.getEndDate2() + " 23:59:59','yyyy-MM-dd HH24:mi:ss'),");  
  12.         sqlstr.append("'" + notice.getCreater() + "',");  
  13.         sqlstr.append("sysdate,");  
  14.         sqlstr.append("empty_clob()");//插入空值  
  15.         sqlstr.append(")");  
  16.   
  17.         String sql = sqlstr.toString();  
  18.         Class.forName(driverClassName);  
  19.         Connection con = DriverManager.getConnection(url, username, password);        
  20.         con.setAutoCommit(false);//设置不自动提交  
  21.         PreparedStatement pstmt = con.prepareStatement(sql);   
  22.         String content = notice.getNoticeContent().replace('\'', '\"');  
  23.           
  24.         try {  
  25.             pstmt.execute();              
  26.                     con.commit();//插入  
  27.               
  28.                 pstmt = con.prepareStatement("select notice_content from notice where notice_id="+notice.getNoticeId()+" for update");//查找刚刚插入的那条记录 for update  
  29.                     ResultSet res = pstmt.executeQuery();  
  30.                     CLOB clob = null;  
  31.                     while(res.next()){  
  32.                         clob = (oracle.sql.CLOB)res.getClob(1);  
  33.                         clob.putString(1, content);//content为前台提交过来的公告内容,大数据量  
  34.                     }  
  35.                
  36.                     pstmt = con.prepareStatement("update notice set notice_content = ? where notice_id=?");//修改notice_content字段的内容  
  37.                     pstmt.setClob(1, clob);  
  38.                     pstmt.setLong(2, notice.getNoticeId());  
  39.                 pstmt.executeUpdate();  
  40.                
  41.                     con.commit();  
  42.               
  43.             if (nps != null && nps.size() > 0) {  
  44.                 noticePartnerDao.saveOrUpdateAll(nps);  
  45.             }  
  46.   
  47.         } catch (Exception e) {  
  48.             info.setCode("1");  
  49.             info.setFlag(false);  
  50.             info.setInfo("发布公告失败!");  
  51.   
  52.             System.out.println(e.getMessage());  
  53.             System.out.println(e.getStackTrace());  
  54.             FileLog.errorLog(e, "发布公告失败");  
  55.             return info;  
  56.         } finally {  
  57.             if(pstmt!=null) {  
  58.                 pstmt.close();                
  59.             }  
  60.             if(con!=null) {  
  61.                 con.close();                  
  62.             }  
  63.         }  


 



对于clob的修改,可在修改该表的其他字段信息时同时将clob字段修改为EMPTY_CLOB (),然后才对clob字段单独修改,方法与上相同。



存入clob类型的文字一般很多,页面不会直接使用textarea,可以使用开源的CKEditor文字编辑器代替,使用方法很简单很实用,详见:CKEditor的使用示例






    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多