java中操作CLOB字段

1、往数据库中插入一个新的CLOB对象    
  public   static   void   clobInsert(String   infile)   throws   Exception    
  {    
  /*   设定不自动提交   */    
  boolean   defaultCommit   =   conn.getAutoCommit();    
  conn.setAutoCommit(false);    
  try   {    
  /*   插入一个空的CLOB对象   */    
  stmt.executeUpdate("INSERT   INTO   TEST_CLOB   VALUES   (’111’,   EMPTY_CLOB())");    
  /*   查询此CLOB对象并锁定   */    
  ResultSet   rs   =   stmt.executeQuery("SELECT   CLOBCOL   FROM   TEST_CLOB   WHERE   ID=’111’   FOR   UPDATE");    
  while   (rs.next())   {    
  /*   取出此CLOB对象   */    
  oracle.sql.CLOB   clob   =   (oracle.sql.CLOB)rs.getClob("CLOBCOL");    
  /*   向CLOB对象中写入数据   */    
  BufferedWriter   out   =   new   BufferedWriter(clob.getCharacterOutputStream());    
  BufferedReader   in   =   new   BufferedReader(new   FileReader(infile));    
  int   c;    
  while   ((c=in.read())!=-1)   {    
  out.write(c);    
  }    
  in.close();    
  out.close();    
  }    
  /*   正式提交   */    
  conn.commit();    
  }   catch   (Exception   ex)   {    
  /*   出错回滚   */    
  conn.rollback();    
  throw   ex;    
  }    
  /*   恢复原提交状态   */    
  conn.setAutoCommit(defaultCommit);    
  }    
  2、修改CLOB对象(是在原CLOB对象基础上进行覆盖式的修改)    
  public   static   void   clobModify(String   infile)   throws   Exception    
  {    
  /*   设定不自动提交   */    
  boolean   defaultCommit   =   conn.getAutoCommit();    
  conn.setAutoCommit(false);    
  try   {    
  /*   查询CLOB对象并锁定   */    
  ResultSet   rs   =   stmt.executeQuery("SELECT   CLOBCOL   FROM   TEST_CLOB   WHERE   ID=’111’   FOR   UPDATE");    
  while   (rs.next())   {    
  /*   获取此CLOB对象   */    
  oracle.sql.CLOB   clob   =   (oracle.sql.CLOB)rs.getClob("CLOBCOL");    
  /*   进行覆盖式修改   */    
  BufferedWriter   out   =   new   BufferedWriter(clob.getCharacterOutputStream());    
  BufferedReader   in   =   new   BufferedReader(new   FileReader(infile));    
  int   c;    
  while   ((c=in.read())!=-1)   {    
  out.write(c);    
  }    
  in.close();    
  out.close();    
  }    
  /*   正式提交   */    
  conn.commit();    
  }   catch   (Exception   ex)   {    
  /*   出错回滚   */    
  conn.rollback();    
  throw   ex;    
  }    
  /*   恢复原提交状态   */    
  conn.setAutoCommit(defaultCommit);    
  }    
    3、替换CLOB对象(将原CLOB对象清除,换成一个全新的CLOB对象)    
  public   static   void   clobReplace(String   infile)   throws   Exception    
  {    
  /*   设定不自动提交   */    
  boolean   defaultCommit   =   conn.getAutoCommit();    
  conn.setAutoCommit(false);    
  try   {    
  /*   清空原CLOB对象   */    
  stmt.executeUpdate("UPDATE   TEST_CLOB   SET   CLOBCOL=EMPTY_CLOB()   WHERE   ID=’111’");    
  /*   查询CLOB对象并锁定   */    
  ResultSet   rs   =   stmt.executeQuery("SELECT   CLOBCOL   FROM   TEST_CLOB   WHERE   ID=’111’   FOR   UPDATE");    
  while   (rs.next())   {    
  /*   获取此CLOB对象   */    
  oracle.sql.CLOB   clob   =   (oracle.sql.CLOB)rs.getClob("CLOBCOL");    
  /*   更新数据   */    
  BufferedWriter   out   =   new   BufferedWriter(clob.getCharacterOutputStream());    
  BufferedReader   in   =   new   BufferedReader(new   FileReader(infile));    
  int   c;    
  while   ((c=in.read())!=-1)   {    
  out.write(c);    
  }    
  in.close();    
  out.close();    
  }    
  /*   正式提交   */    
  conn.commit();    
  }   catch   (Exception   ex)   {    
  /*   出错回滚   */    
  conn.rollback();    
  throw   ex;    
  }    
  /*   恢复原提交状态   */    
  conn.setAutoCommit(defaultCommit);    
  }    
  4、CLOB对象读取    
  public   static   void   clobRead(String   outfile)   throws   Exception    
  {    
  /*   设定不自动提交   */    
  boolean   defaultCommit   =   conn.getAutoCommit();    
  conn.setAutoCommit(false);    
  try   {    
  /*   查询CLOB对象   */    
  ResultSet   rs   =   stmt.executeQuery("SELECT   *   FROM   TEST_CLOB   WHERE   ID=’111’");    
  while   (rs.next())   {    
  /*   获取CLOB对象   */    
  oracle.sql.CLOB   clob   =   (oracle.sql.CLOB)rs.getClob("CLOBCOL");    
  /*   以字符形式输出   */    
  BufferedReader   in   =   new   BufferedReader(clob.getCharacterStream());    
  BufferedWriter   out   =   new   BufferedWriter(new   FileWriter(outfile));    
  int   c;    
  while   ((c=in.read())!=-1)   {    
  out.write(c);    
  }    
  out.close();    
  in.close();    
  }    
  }   catch   (Exception   ex)   {    
  conn.rollback();    
  throw   ex;    
  }    
  /*   恢复原提交状态   */    
  conn.setAutoCommit(defaultCommit);    
  }    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值