J2EE操作Oracle的clob类型字段

来源:互联网 发布:sql中like的用法 编辑:程序博客网 时间:2024/05/02 01:42

关键字: java

Oracle中,Varchar2支持的最大字节数为4KB,所以对于某些长字符串的处理,我们需要用CLOB类型的字段,CLOB字段最大支持4GB。
还有其他几种类型:
blob:二进制,如果exe,zip
clob:单字节码,比如一般的文本文件.
nlob:多字节码,如UTF格式的文件.
以下就是对CLOG字段的操作方法,在我们的项目中帮助文档部分用到。
1、首先是写入
Java代码 复制代码
  1. /* 以下表PF_HELP_CONTENT中的HCONTENT字段时CLOB类型的 */  
  2. // 通过序列器生成帮助ID    
  3. Map map = Query.getMap("Select TO_CHAR(SEQ_HID.nextval) HID FROM DUAL ");    
  4. hid = String.valueOf(map.get("HID"));    
  5. //插入一条数据,注意CLOB字段,需要先插入一个空的clob类型 empty_clob(),然后再单独更新clob字段    
  6. sql = "Insert INTO PF_HELP_CONTENT(HID,HCONTENT) VALUES (?,empty_clob())  ";    
  7. try  
  8. {              
  9.      //执行插入    
  10.      rtn = DbUtils.executeUpdate(sql,hid);        
  11.      /* 插入成功后,修改HCONTENT字段内容 */  
  12.      //取得数据库连接                             
  13.      Connection conn = DbUtils.getConnection();    
  14.      //手动提交    
  15.      conn.setAutoCommit(false);    
  16.      //定义ResultSet 和 Clob 变量    
  17.      ResultSet rs = null;    
  18.      oracle.sql.CLOB clob = null;    
  19.      //更新SQL    
  20.      String sqlclob = "Select HCONTENT FROM PF_HELP_CONTENT Where HID=? FOR Update ";    
  21.      java.sql.PreparedStatement pstmt = conn.prepareStatement(sqlclob);    
  22.      //hid是varchar2类型的,所以用setString    
  23.      pstmt.setString(1,hid);    
  24.      //执行update语句    
  25.      rs= pstmt.executeQuery();    
  26.      if(rs.next())    
  27.      {    
  28.         //取得刚才的HCONTENT的内容,也就是刚才添加的empty_clob()    
  29.         clob = (oracle.sql.CLOB)rs.getClob(1);    
  30.      }    
  31.      //需要用clob.getCharacterOutputStream()流方式输出    
  32.      Writer write = clob.getCharacterOutputStream();    
  33.      //写入具体内容,helpform.getHContent() 存的是帮助的内容    
  34.      write.write(helpform.getHContent());    
  35.      write.flush();    
  36.      write.close();    
  37.      rs.close();    
  38.      //提交    
  39.      conn.commit();    
  40.      conn.close();    
  41. }    
  42. catch(Exception ex)    
  43. {    
  44.     //.........    
  45. }  

2、修改CLOB字段内容 
Java代码 复制代码
  1. /* 修改跟插入时基本一致,也是用for update来实现 */  
  2. //如果修改前的字段内容长度大于当前修改的长度时,末尾的部分内容仍然会存在    
  3. //所以在修改内容前,需要将PF_HELP_CONTENT内容置空    
  4. sql = " Update PF_HELP_CONTENT SET HCONTENT=empty_clob() Where HID=? ";    
  5. try  
  6. {          
  7.  rtn = DbUtils.executeUpdate(sql,hid);    
  8.  //以下操作跟添加时一样                                   
  9.  Connection conn = DbUtils.getConnection();    
  10.  conn.setAutoCommit(false);    
  11.  ResultSet rs = null;    
  12.  oracle.sql.CLOB clob = null;    
  13.  String sqlclob = "Select HCONTENT FROM PF_HELP_CONTENT Where HID=? FOR Update ";    
  14.  java.sql.PreparedStatement pstmt = conn.prepareStatement(sqlclob);    
  15.  pstmt.setString(1,hid);    
  16.  rs= pstmt.executeQuery();    
  17.  if(rs.next())    
  18.  {    
  19.     clob = (oracle.sql.CLOB)rs.getClob(1);    
  20.  }    
  21.  Writer write = clob.getCharacterOutputStream();    
  22.  write.write(helpform.getHContent());    
  23.  write.flush();    
  24.  write.close();    
  25.  rs.close();    
  26.  conn.commit();    
  27.  conn.close();                                    
  28. }    
  29. catch(Exception ex)    
  30. {    
  31.   //...    
  32. }  


3、取出CLOB字段的文本内容
Java代码 复制代码
  1. /* 前面部分都一致 */  
  2. Connection conn = DbUtils.getConnection();    
  3. conn.setAutoCommit(false);    
  4. ResultSet rs = null;    
  5. oracle.sql.CLOB clob = null;    
  6. String sqlclob = "Select HCONTENT FROM PF_HELP_CONTENT Where HID=? ";    
  7. java.sql.PreparedStatement pstmt = conn.prepareStatement(sqlclob);    
  8. pstmt.setString(1,hid);    
  9. rs= pstmt.executeQuery();    
  10. if(rs.next())    
  11. {    
  12.     //rs.getClob(1)中参数1指的是HCONTENT字段索引,第一个字段从1开始而不是从0。    
  13.     //也可以用字段名来取rs.getClob("HCONTENT")    
  14.     clob = (oracle.sql.CLOB)rs.getClob(1);    
  15. }    
  16. if(clob==null || clob.length()==0)    
  17. {    
  18.     hcontent = "";    
  19. }else  
  20. {    
  21.     //取CLOB字段内容为字符串    
  22.     hcontent=clob.getSubString((long)1,(int)clob.length());    
  23. }    
  24. rs.close();    
  25. conn.close();    
  26. request.setAttribute("HCONTENT",hcontent);  
原创粉丝点击