读取简单点,用到输入输出流。
1
privateString getClob (CLOB clob)throwsException2
{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*/1516 String content="";1718 Reader is=clob.getCharacterStream();19 BufferedReader br=newBufferedReader(is);20 String s=br.readLine();21while(s!=null)22
{23 content+=s+"\r\n";24 s=br.readLine();25 }2627returncontent;2829}
下面是写入CLOB
1
//the first, run2//ResultSet rs = null;3//String sSQL = "select " + ColName + " from " + tableName + " where id= '" + ID + "' for update";4//(oracle.sql.CLOB)rs.getClob(number);5privatevoidfillClob (CLOB clob, String data)throwsException6
{7if(data==null)8
{9 data="no value !";10 }11if(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锁定
1
strSql="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;1112strSql="update tableName t set t.CLOB_column, t.date = to_date('"+sdate+"','YYYY-MM-DD') where t.id ="+id;13pstm=this.conn.prepareStatement(strSql);14intaflag=pstm.executeUpdate();15conn.commit();1617rs.close();18pstm.close();
posted on 2007-04-27 09:39 hiker 阅读(598) 评论(1) 编辑 收藏 所属分类: oracle