结合实践的可行的一种对Oracle Clob大文本字段的存取方法之一

写入 方法

      String inTime = new Timestamp(System.currentTimeMillis()).toString();

      String sql = "INSERT INTO LWC.TAB_USER_SAY (ID_USER_SAY, MESSAGE_TITLE, MESSAGE_CONTENT, MSSSAGE_NAME, MSSSAGE_TEL, MESSAGE_EMAIL,MESSAGE_ADDRESS, MESSAGE_ZIP, OK_FLAG, IN_TIME) VALUES (LWC.SEQ_USER_SAY.NEXTVAL,'" +
          DBUtil.FormatSQLValue(userSayForm.getMessageTitle()) + "',empty_clob(),'" + DBUtil.FormatSQLValue(userSayForm.getMessageName()) + "','" +
          DBUtil.FormatSQLValue(userSayForm.getMessageTel()) + "','" + DBUtil.FormatSQLValue(userSayForm.getMessageEmail())+ "','" +
          DBUtil.FormatSQLValue(userSayForm.getMessageAddress()) +"','" +
          DBUtil.FormatSQLValue(userSayForm.getMessageZip()) +
          "','0','" + inTime + "')";

      logger.info(sql);     
      //提交Clob字段内容
      //'" + DBUtil.FormatSQLValue(userSayForm.getMessageContent()) + "',
      conn.setAutoCommit(false);     
      stat.executeUpdate(sql);     
      String sqll="select MESSAGE_CONTENT from LWC.TAB_USER_SAY where IN_TIME='"+inTime+"' for update";
      ResultSet rss=stat.executeQuery(sqll);
      if(rss.next()){
       oracle.sql.CLOB clob = ((oracle.jdbc.OracleResultSet)rss).getCLOB(1);
       clob.putString(1,DBUtil.FormatSQLValue(userSayForm.getMessageContent()));
       sql="update LWC.TAB_USER_SAY set MESSAGE_CONTENT=? where IN_TIME='"+inTime+"'";
       PreparedStatement pstmt=conn.prepareStatement(sql);
       pstmt.setClob(1,clob);
       pstmt.executeUpdate();
       pstmt.close();
      }     
      conn.commit();
      stat.close();
      conn.close();

补充一句

如果是update 则应将原数据库中的 clob字段 update 成为 empty_clob()

然后通过 读取到 rs的 clob 中之后 再 update 新的内容

 

读取方法

    //读取大文本
    String content = "";
    oracle.sql.CLOB x = ( (oracle.jdbc.driver.OracleResultSet) result).getCLOB("ORG_INTRO");
    if (x != null) {
      Reader is = x.getCharacterStream();
      BufferedReader br = new BufferedReader(is);
      String s = br.readLine();
      while (s != null) {
        content += s + "/r/n";
        s = br.readLine();
      }
    }

可参考的几个WEB

http://blog.csdn.net/etre/archive/2003/07/20/17560.aspx

http://www.newer.com.cn/newer/bbs/topic.asp?topic_id=5270&cat_id=11&forum_id=19

www.itpub.net

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值