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);//取消自動提交
          需要被首先執行

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

          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 閱讀(680) 評論(1)  編輯  收藏 所屬分類: oracle

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

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


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

          常用鏈接

          留言簿(1)

          隨筆分類(5)

          隨筆檔案(63)

          文章分類(3)

          文章檔案(3)

          最新隨筆

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 志丹县| 治多县| 和田县| 安化县| 西畴县| 额尔古纳市| 姜堰市| 聂拉木县| 阿拉善盟| 来宾市| 徐闻县| 禄丰县| 文昌市| 威远县| 漯河市| 耒阳市| 龙岩市| 卢氏县| 乌兰察布市| 南江县| 尤溪县| 迁安市| 和平区| 丰台区| 万宁市| 淮安市| 巧家县| 利津县| 无锡市| 萨嘎县| 土默特左旗| 禄劝| 浪卡子县| 宜兴市| 珠海市| 石林| 闻喜县| 建德市| 柘城县| 平邑县| 云霄县|