关闭

运用Java如何存取Oracle中的CLOB类型字

922人阅读 评论(0) 收藏 举报

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{
  Class.forName("oracle.jdbc.driver.OracleDriver");
  Connection con=DriverManager.getConnection("jdbc:oracle:thin:@fangm:1521:LICSFC","SFC","SFC");
  //con.setAutoCommit(false);
  //Ok 1
  String sql="insert into test values(1,empty_clob())";
  Statement stmt=con.createStatement();
  ResultSet rs=stmt.executeQuery(sql);
  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);
   clob.putString(1,"ddddddddddddddddddddddddddddddddddd");
   sql="update test set content=? where id=1";
   PreparedStatement pstmt=con.prepareStatement(sql);
   pstmt.setClob(1,clob);
   pstmt.executeUpdate();
   pstmt.close();
  }
  //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");
  Connection con=DriverManager.getConnection("jdbc:oracle:thin:@fangm:1521:LICSFC","SFC","SFC");
  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("query:"+ee);
  }
  
 }
 }


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3283083次
    • 积分:27942
    • 等级:
    • 排名:第198名
    • 原创:334篇
    • 转载:552篇
    • 译文:7篇
    • 评论:331条
    最新评论
    艺术设计类