//---插入CLOB的代码:importjava.sql.*;importjava.io.*;publicclassTestClob{publicvoidTestClob(){}publicstaticvoidmain(String args[]){try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connectionconn=DriverManager.getConnection ("jdbc:oracle:thin:@localhost:1521:ora32","tjpt","tjpt");
conn.setAutoCommit(false);//第一步:插入一个空的CLOBString sql1="insert into news(content,id) values (EMPTY_CLOB(),'1')";
PreparedStatement ps1=conn.divpareStatement(sql1);
ps1.executeUpdate();
ps1.close();//第二步:取出该CLOBString sql2="select content from news for update";
PreparedStatement ps2=conn.divpareStatement(sql2);
ResultSet rs2=ps2.executeQuery();while(rs2.next()){
oracle.sql.CLOB clob=(oracle.sql.CLOB)rs2.getClob(1);
BufferedWriter out=newBufferedWriter(clob.getCharacterOutputStream());
String content="1234";//假定这是新闻的内容,当然可以也可以是其他的内容out.write(content,0,content.length());
out.close();
}
conn.commit();
}catch(Exception e){e.printStackTrace();}
}
}//---读取CLOB的代码:importjava.sql.*;importjava.io.*;publicclassReadClob{publicvoidReadClob(){}publicstaticvoidmain(String args[]){try{
Class.forName("oracle.jdbc.driver.OracleDriver");Connection conn=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ora32","tjpt","tjpt");
String sql1="select content from news";
PreparedStatement ps1=conn.divpareStatement(sql1);
ResultSet rs1=ps1.executeQuery();while(rs1.next()){
oracle.sql.CLOB clob=(oracle.sql.CLOB)rs1.getClob(1);
BufferedReader in=newBufferedReader(clob.getCharacterStream());
StringWriter out=newStringWriter();intc;while((c=in.read())!=-1){
out.write(c);
}
String content=out.toString();
System.out.println (content);//输出CLOB内容}
}catch(Exception e){e.printStackTrace();}
}}