clob字段存入数据库和从数据库取出的方法
1.clob字段存入数据库的方法:
下面这个例子中content字段是clob类型的
StringBuffer content=new StringBuffer(request.getParameter("content").trim());
try{
Statement stat=con.createStatement();
String sql="insert into jwk_forum(bh,speaker,title,content,provide_time,reply_num,click_num,parent_no,tutor) values(jwk_forum_bh_seq.nextVal,'"+speaker+"','"+title+"',EMPTY_CLOB(),Sysdate,'0','0','0','"+tutor+"')";
boolean defaultCommit = con.getAutoCommit();
con.setAutoCommit(false);
stat.executeUpdate(sql);
int bh=0;
ResultSet rs1=stat.executeQuery("select max(bh) from jwk_forum");
while(rs1.next()) bh=rs1.getInt(1);
ResultSet rs = stat.executeQuery("select content from jwk_forum where bh='"+bh+"' FOR UPDATE");
while (rs.next())
{
// 取出此CLOB对象
oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob(1);
// 向CLOB对象中写入数据
BufferedWriter out1 = new BufferedWriter(clob.getCharacterOutputStream());
int i=0;
while(i+1500<content.length())
{
out1.write(content.substring(i,i+1500),0,1500);
i+=1500;
}
out1.write(content.substring(i,content.length()),0,content.length()-i);
out1.close();
}
con.commit();
}catch (Exception e){
out.println(e.getMessage());
}
finally{
con.close();
}
2.clob字段从数据库取出的方法:
下面是从数据库取出特定编号的记录的content字段
String content="";
try{
Statement stmt=con.createStatement();
String sql="select content from jwk_forum where
bh='"+bh+"'";
ResultSet rs=stmt.executeQuery(sql);
if(rs.next())
{
Clob clob = rs.getClob(4);
if(clob!=null){
Reader is = clob.getCharacterStream();
BufferedReader br = new BufferedReader(is);
String s = br.readLine();
while(s!=null){
content += s+"\n";
s=br.readLine();
}
}
if(content!=null)
{
java.util.StringTokenizer st=new
java.util.StringTokenizer(content,"\n");
String _content=" ";
content=_content;
while(st.hasMoreTokens())
{
content+=st.nextToken()+"<br>    ";
}
}
}