J2EE操作Oracle的clob類型字段

Oracle中,Varchar2支持的最大字節數爲4KB,所以對於某些長字符串的處理,我們需要用CLOB類型的字段,CLOB字段最大支持4GB。 
還有其他幾種類型: 
blob:二進制,如果exe,zip 
clob:單字節碼,比如一般的文本文件. 
nlob:多字節碼,如UTF格式的文件. 
以下就是對CLOG字段的操作方法,在我們的項目中幫助文檔部分用到。 

1、首先是寫入 


  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字段內容  

  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字段的文本內容 

  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);  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章