ORacle中Clob字段操作

J2EE操作Oracle的clob类型字段
关键字: 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. } 

/* 以下表PF_HELP_CONTENT中的HCONTENT字段时CLOB类型的 */
// 通过序列器生成帮助ID
Map map = Query.getMap("Select TO_CHAR(SEQ_HID.nextval) HID FROM DUAL ");
hid = String.valueOf(map.get("HID"));
//插入一条数据,注意CLOB字段,需要先插入一个空的clob类型 empty_clob(),然后再单独更新clob字段
sql = "Insert INTO PF_HELP_CONTENT(HID,HCONTENT) VALUES (?,empty_clob())  ";
try
{          
     //执行插入
     rtn = DbUtils.executeUpdate(sql,hid);    
     /* 插入成功后,修改HCONTENT字段内容 */
     //取得数据库连接                         
     Connection conn = DbUtils.getConnection();
     //手动提交
     conn.setAutoCommit(false);
     //定义ResultSet 和 Clob 变量
     ResultSet rs = null;
     oracle.sql.CLOB clob = null;
     //更新SQL
     String sqlclob = "Select HCONTENT FROM PF_HELP_CONTENT Where HID=? FOR Update ";
     java.sql.PreparedStatement pstmt = conn.prepareStatement(sqlclob);
     //hid是varchar2类型的,所以用setString
     pstmt.setString(1,hid);
     //执行update语句
     rs= pstmt.executeQuery();
     if(rs.next())
     {
        //取得刚才的HCONTENT的内容,也就是刚才添加的empty_clob()
        clob = (oracle.sql.CLOB)rs.getClob(1);
     }
     //需要用clob.getCharacterOutputStream()流方式输出
     Writer write = clob.getCharacterOutputStream();
     //写入具体内容,helpform.getHContent() 存的是帮助的内容
     write.write(helpform.getHContent());
     write.flush();
     write.close();
     rs.close();
     //提交
     conn.commit();
     conn.close();
}
catch(Exception ex)
{
    //.........
}


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. } 

/* 修改跟插入时基本一致,也是用for update来实现 */
//如果修改前的字段内容长度大于当前修改的长度时,末尾的部分内容仍然会存在
//所以在修改内容前,需要将PF_HELP_CONTENT内容置空
sql = " Update PF_HELP_CONTENT SET HCONTENT=empty_clob() Where HID=? ";
try
{      
 rtn = DbUtils.executeUpdate(sql,hid);
 //以下操作跟添加时一样                               
 Connection conn = DbUtils.getConnection();
 conn.setAutoCommit(false);
 ResultSet rs = null;
 oracle.sql.CLOB clob = null;
 String sqlclob = "Select HCONTENT FROM PF_HELP_CONTENT Where HID=? FOR Update ";
 java.sql.PreparedStatement pstmt = conn.prepareStatement(sqlclob);
 pstmt.setString(1,hid);
 rs= pstmt.executeQuery();
 if(rs.next())
 {
    clob = (oracle.sql.CLOB)rs.getClob(1);
 }
 Writer write = clob.getCharacterOutputStream();
 write.write(helpform.getHContent());
 write.flush();
 write.close();
 rs.close();
 conn.commit();
 conn.close();                                
}
catch(Exception ex)
{
  //...
}

 

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

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/mader/archive/2008/12/04/3445578.aspx

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值