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 阅读(655) 评论(1)  编辑  收藏 所属分类: oracle

FeedBack:
# re: oracle9i中关于CLOB字段的读写
2007-04-27 14:24 | hiker
我自己回复下。
当我copy代码的时候,发现行号也包括进去了!
这个代码编辑器不好!  回复  更多评论
  

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


网站导航:
 
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(1)

随笔分类(5)

随笔档案(63)

文章分类(3)

文章档案(3)

最新随笔

搜索

  •  

最新评论

阅读排行榜

评论排行榜