先插一个空的clob对象,然后选出来进行读写

解决方案 »

  1.   


    import java.io.*;import oracle.jdbc.*;
    import oracle.jdbc.driver.*;
    import java.sql.*;public class OraClob {
    public static void main(String[] args) {
    try{
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection  conn=DriverManager.getConnection("jdbc:oracle:oci8:@sid", "username", "password");
    conn.setAutoCommit(false);
    String sql = "insert into clobdata values(2, empty_clob())";
    //Statement stmt = conn.createStatement ();
    PreparedStatement stmt = conn.prepareStatement(sql);
    //Statement stmt = conn.createStatement();
    //stmt.executeUpdate(sql);
    stmt.execute(sql);
    //conn.commit();           
    String sql2="select content from clobdata where id=2 for update";
    ResultSet rs = stmt.executeQuery(sql2);
    System.out.println("inserting...");
    if(rs.next()){
    File originVR=new File("/home/oracle/pro.txt");
    oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("content");
    /*先建立一个由文件而来的输入流,然后把它一行一行的读入到clob中
     * 
     * */
      BufferedReader in = new BufferedReader(new InputStreamReader(
      new BufferedInputStream(
    new FileInputStream(originVR))));
     

    /*
     * 把输入流中的内容转入clob的输出流
     * */

            StringBuffer strBuf=new StringBuffer();
            //String bc=null;
            System.out.println("swapping the chars from file to clob");
    //int i=0;
    long =1;
    String tmp="";

    while((tmp = in.readLine())!=null){
    // write this line of chars into a string buffer
    System.out.println(tmp);
    //clob.putString(,tmp);
    clob.putString(,tmp);
     +=tmp.length();

    }


    // clob.putString(1,"hello world this si the first test of the jdbc of clob");
        // clob.pubString(1,in.readLine());
        // clob.putString(1,in.readLine());
    // put the stringBuffer's contents chars to the clob object 
    //clob.putString(1,strBuf.toString());
    System.out.println("finished the swaping work!");


    sql = "update clobdata set content=? where id=2";
    PreparedStatement pstmt   = conn.prepareStatement(sql);
    pstmt.setClob(1, clob);

    pstmt.executeUpdate();

    System.out.println("finished the insert work from the file to clob in oracle .");

    System.out.println("the clob area is longth :"+clob.length());
    in.close();
    conn.close();
    //outClob.close();


    }
    }catch(SQLException e){
    System.out.println("mistake when operating the database!");
    System.out.println(e.getMessage());
    }
    catch(FileNotFoundException e){
    System.out.println("the file can't be find!");
    System.out.println(e.getMessage());
    }catch(ClassNotFoundException e){
    System.out.println("the oracle jdbc is not found!");
    System.out.println(e.getMessage());
    }catch (IOException e){
    System.out.println("the file stream can't cractly excute!");
    }catch (Exception e){
    System.out.println("other exception happens!");
    System.out.println(e.getMessage());
    }
    }
    }