将java Object存储为clob并检索

时间:2014-02-18 12:47:53

标签: java oracle jdbc clob

我在oracle中有一张桌子。在该表中,其中一列是存储clob数据。在那个clob列中, 必须插入对象作为clob。

该对象应保存为clob,并且必须再次作为对象进行检索。

在java中有任何可能将Obect存储为clob。

我按照link

尝试了这个例子
import java.sql.*;   
import java.io.*;   
import oracle.jdbc.driver.OracleResultSet;  
import oracle.sql.CLOB;  
public class TestOracleClob implements Serializable{   
 public static void main(String[] args)   
 {   

  //create table test (id integer,content clob);  
  System.out.println("-------------------insert -----------------");  
  try{  
   DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());  
Connection con = DriverManager.getConnection ("oracle server",  
   "loginid", "loginpassword");   
  //Class.forName("oracle.jdbc.driver.OracleDriver");  

  con.setAutoCommit(false);  
  //Ok 1  
  String sql="insert into test values(1,empty_clob())";  
  Statement stmt=con.createStatement();  
  ResultSet rs=stmt.executeQuery(sql);  
  System.out.println("-------------------insert -----------------");  
  String sqll="select content from test where id=1 for update";      
  ResultSet rss=stmt.executeQuery(sqll);  

  if(rss.next()){  
   //CLOB clob = ((OracleResultSet)rss).getCLOB(1);  
 oracle.sql.CLOB  clob= (oracle.sql.CLOB)rss.getClob("content");  
   clob.putString(1,"here is a string which contains more than 4000 character");  
   sql="update test set content=? where id=1";  
   PreparedStatement pstmt=con.prepareStatement(sql);  
   pstmt.setClob(1,clob);  
   pstmt.executeUpdate();  
   pstmt.close();  
  }    
  con.commit();  

  //Ok 2  
  //String sql1="insert into test values(2,empty_clob())";  
  //ResultSet rs3=stmt.executeQuery(sql1);  
  String sql12="insert into test values(?,?)";  
  PreparedStatement pstmt1=con.prepareStatement(sql12);  
  pstmt1.setInt(1,2);  
  pstmt1.setClob(2,oracle.sql.CLOB.empty_lob());  
  pstmt1.executeUpdate();  

  String sqll2="select content from test where id=2 for update";  
  ResultSet rss2=stmt.executeQuery(sqll2);  
  if(rss2.next()){  
   CLOB clob = ((OracleResultSet)rss2).getCLOB(1);  
   clob.putString(1,"affffffffffdfdfdfdddddddffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffdddfff");  
   String sql1="update test set content=? where id=2";  
   PreparedStatement pstmt=con.prepareStatement(sql1);  
   pstmt.setClob(1,clob);  
   pstmt.executeUpdate();  
   pstmt.close();  
  }  
  con.commit();  
  rss.close();  
  rss2.close();  
  pstmt1.close();  
  rs.close();  
  stmt.close();  
  con.close();  
  System.out.println("-------------insert ok-------------");  
  }catch(Exception e){  
   System.out.println("insert:"+e);  
  }  
  System.out.println("-------------------query -----------------");  
  try{  
  String content="";  
  //Class.forName("oracle.jdbc.driver.OracleDriver");  
  DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());  
  Connection con = DriverManager.getConnection ("oracleserver",  
   "id", "pass");   

  Statement stmt=con.createStatement();  
  String sql="select content from test where id=1";  
  ResultSet rs=stmt.executeQuery(sql);  
  if(rs.next()){  
   CLOB clob = ((OracleResultSet)rs).getCLOB(1);  
   if(clob!=null){  
   Reader is=clob.getCharacterStream();  
   BufferedReader br=new BufferedReader(is);  
   String s=br.readLine();  
   while(s!=null){  
    content+=s+",";  
    s=br.readLine();  
    }  
   }  

  }  
  rs.close();  
  stmt.close();  
  con.close();  
  System.out.println("clob:"+content);  
  System.out.println("-------------query ok-------------");  
  }catch(Exception ee){  
   System.out.println("Exception:"+ee);  
  }  

 }  
 } 

但是他们将String添加到clob。但我想将我的对象添加到clob中。有可能吗?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我想您可以直接将对象序列化为clob,如下所示:

Object o = new Object(); // anything serializable
ObjectOutputStream out = new ObjectOutputStream(clob.setAsciiStream(0));
out.writeObject(o);
out.close();

读反之亦然:

ObjectInputStream in = new ObjectInputStream(clob.getAsciiStream());
Object o = in.readObject();
in.close();