posts - 63,  comments - 7,  trackbacks - 0

          讀取簡單點,用到輸入輸出流。

           1private String getClob (CLOB clob) throws Exception
           2{
           3    //2種寫法
           4    /**
           5    oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob(1);
           6    BufferedReader in = new BufferedReader(clob.getCharacterStream());                      
           7    StringWriter out=new StringWriter();                      
           8    int c;                      
           9    while((c=in.read())!=-1)
          10    {                      
          11        out.write(c);                                  
          12    }                 
          13    String content=out.toString();
          14    */

          15    
          16    String content = "";
          17     
          18    Reader is = clob.getCharacterStream();
          19    BufferedReader br = new BufferedReader(is);
          20    String s = br.readLine();
          21    while (s != null
          22    {
          23        content += s + "\r\n";
          24        s = br.readLine();
          25    }

          26       
          27    return content;
          28
          29}

          下面是寫入CLOB

           1    //the first, run 
           2    //ResultSet rs = null;
           3    //String sSQL = "select " + ColName + " from " + tableName + " where id= '" + ID + "' for update";
           4    //(oracle.sql.CLOB)rs.getClob(number);
           5      private void fillClob (CLOB clob, String data) throws Exception
           6      {
           7        if (data == null
           8        {      
           9            data = "no value !";    
          10        }

          11        if (clob != null
          12        {
          13            Writer wr = clob.getCharacterOutputStream();
          14            wr.write(data);
          15            wr.flush();
          16            wr.close();
          17         }

          18      }

           
          conn.setAutoCommit(false);//取消自動提交
          需要被首先執(zhí)行

          需要插入一條新的記錄時,可以像下面這樣:

          1. 先取sequence的值
                strSql = "select sequence(表中column的名字).nextval from dual";
                pstm = this.conn.prepareStatement(strSql);
                rs = pstm.executeQuery();

          2. 插入一個空值的CLOB
                insert into tableName t (t.CLOB_column, t.sequence) values(empty_clob(), id)
                pstm.executeUpdate();

          3. 把這一行鎖定,用select ...for update語句,然后在寫入
                strSql = "select t.CLOB_column from tableName t where t.sequence= " + id + " for update";
                
                pstm = this.conn.prepareStatement(strSql);
                rs = pstm.executeQuery();
               
                if (rs.next()) 
                 {
                      clob1= (oracle.sql.CLOB) rs.getClob(1);
                       fillClob(clob1, content);
                 }
                  
                rs.close();
                conn.commit();//對應上面的那句
                pstm.close();

          更新一條記錄

          1. 清空CLOB的內容

                strSql = "update tableName t  set t.CLOB_column = empty_clob() where t.id ='" + id + "'";
                pstm = this.conn.prepareStatement(strSql);
                pstm.executeUpdate();

          2. 和插入新記錄一樣,需要用for update鎖定

           1strSql = "select t.CLOB_column from tableName t where t. ='" + id + "' for update";
           2pstm = this.conn.prepareStatement(strSql);
           3rs = pstm.executeQuery();
           4while (rs.next()) 
           5{
           6    clob = (oracle.sql.CLOB) rs.getClob(3);
           7    fillClob(clob, content);
           8    conn.commit();
           9}

          10pstm = null;
          11
          12strSql = "update tableName t set t.CLOB_column, t.date = to_date('" + sdate + "','YYYY-MM-DD') where t.id = " + id; 
          13pstm = this.conn.prepareStatement(strSql);
          14int aflag = pstm.executeUpdate(); 
          15conn.commit();
          16
          17rs.close();
          18pstm.close();
          posted on 2007-04-27 09:39 hiker 閱讀(677) 評論(1)  編輯  收藏 所屬分類: oracle

          FeedBack:
          # re: oracle9i中關于CLOB字段的讀寫
          2007-04-27 14:24 | hiker
          我自己回復下。
          當我copy代碼的時候,發(fā)現(xiàn)行號也包括進去了!
          這個代碼編輯器不好!  回復  更多評論
            

          只有注冊用戶登錄后才能發(fā)表評論。


          網站導航:
           
          <2007年4月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(1)

          隨筆分類(5)

          隨筆檔案(63)

          文章分類(3)

          文章檔案(3)

          最新隨筆

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 敖汉旗| 溧水县| 伊宁县| 礼泉县| 浦江县| 丹凤县| 新乡市| 沾益县| 特克斯县| 寻乌县| 汉源县| 措美县| 景洪市| 霍州市| 阳泉市| 桦南县| 黑龙江省| 衡水市| 观塘区| 正蓝旗| 青河县| 宁晋县| 重庆市| 临沧市| 垦利县| 涞水县| 雷州市| 佳木斯市| 灵武市| 文成县| 拜城县| 彰武县| 布拖县| 彩票| 南漳县| 云龙县| 迁安市| 沭阳县| 普兰店市| 建德市| 长丰县|