小阁飞空 一池碧映垂杨路 绛云深处 听尽潇潇雨
At times , people will simply not come through for you in the way you need.Forgive them and move on.
posts - 212,comments - 87,trackbacks - 0
在Oracle中,LOB(Large Object,大型对象)类型的字段现在用得越来越多了。因为这种类型的字段,容量大(最多能容纳4GB的数据),且一个表中可以有多个这种类型的字段,很灵活,适用于数据量非常大的业务领域(如图象、档案等)。而LONG、LONG RAW等类型的字段,虽然存储容量也不小(可达2GB),但由于一个表中只能有一个这样类型的字段的限制,现在已很少使用了。

LOB类型分为BLOB和CLOB两种:BLOB即二进制大型对象(Binary Large Object),适用于存贮非文本的字节流数据(如程序、图象、影音等)。而CLOB,即字符型大型对象(Character Large Object),则与字符集相关,适于存贮文本型的数据(如历史档案、大部头著作等)。

对于一般的java应用程序,clob类型的数据用对象形式插入即可,连接方式采用Oracle Thin JDBC Driver
注意:存取操作开始前,必须用setAutoCommit(false)取消自动提交,否则Oracle将抛出“读取违反顺序”的错误。

取: ...
        con.setAutoCommit(false);
....
        if(rss.next())
        {
          Clob clob = rss.getClob(1);
          if(clob!=null)
   //直接转换为String返回,也可使用getCharacterOutputStream()或getAsciiOutputStream()
   //
   String str=clob.getSubString((long) 1, (int) clob.length());
       }
      
写:LOB类型字段,则只能用SELECT … FOR UPDATE命令将记录查询出来并锁定,然后才能修改
import oracle.sql.CLOB;
OracleResultSet rss=(OracleResultSet)stmt.executeQuery("select my_clob from test where id=1 for update"); 
if(rss.next())
  {
       CLOB clob=(CLOB)rss.getClob(1);
//content为大于4k 的String,当然也可使用流形式写入
       clob.putString(1,content);
      .....
      OraclePreparedStatement pstmt=(OraclePreparedStatement)con.prepareStatement("update test set my_clob=?");
      pstmt.setClob(1,(Clob)clob);
        pstmt.executeUpdate();
...
}


但同样的方法在BEA WLS 8.1SP2上取数据没有问题,写CLOB会有问题的,会报一种这样的错误:java.lang.ClassCastException
对此bea 是这样解释并解决的:
For most extensions that Oracle provides, you can use the standard technique as described in Using Vendor Extensions to JDBC Interfaces. However, the Oracle Thin driver does not provide public interfaces for its extension methods in the following classes:

oracle.sql.ARRAY
oracle.sql.STRUCT
oracle.sql.REF
oracle.sql.BLOB
oracle.sql.CLOB
WebLogic Server provides its own interfaces to access the extension methods for those classes:

weblogic.jdbc.vendor.oracle.OracleArray
weblogic.jdbc.vendor.oracle.OracleStruct
weblogic.jdbc.vendor.oracle.OracleRef
weblogic.jdbc.vendor.oracle.OracleThinBlob
weblogic.jdbc.vendor.oracle.OracleThinClob

意思即是采用weblogic.jdbc.vendor.oracle.OracleThinClob 替代oracle.sql.CLOB

修改后的写CLOB字段,只用修改一句,其他不变。
weblogic.jdbc.vendor.oracle.OracleThinClob  clob=(OracleThinClob)rss.getClob(1);
posted on 2005-11-16 14:35 潇潇雨 阅读(1054) 评论(1)  编辑  收藏 所属分类: JAVADatabase

FeedBack:
# re: oracle数据库中CLOB字段的处理问题
2006-09-25 15:01 | 姚文杰
呵呵,我就刚好遇到这个问题,万分感谢你的文章
  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: