http://blog.csdn.net/sanuxd/archive/2004/09/09/blob.aspx

解决方案 »

  1.   

    一、 CLOB对象的存取1、往数据库中插入一个新的CLOB对象public static void clobInsert(String infile) throws Exception
    {
    /* 设定不自动提交 */
    boolean defaultCommit = conn.getAutoCommit();
    conn.setAutoCommit(false);try {
    /* 插入一个空的CLOB对象 */
    stmt.executeUpdate("INSERT INTO TEST_CLOB VALUES ('111', EMPTY_CLOB())");
    /* 查询此CLOB对象并锁定 */
    ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
    while (rs.next()) {
    /* 取出此CLOB对象 */
    oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
    /* 向CLOB对象中写入数据 */
    BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
    BufferedReader in = new BufferedReader(new FileReader(infile));
    int c;
    while ((c=in.read())!=-1) {
    out.write(c);
    }
    in.close();
    out.close();
    }
    /* 正式提交 */
    conn.commit();
    } catch (Exception ex) {
    /* 出错回滚 */
    conn.rollback();
    throw ex;
    }/* 恢复原提交状态 */
    conn.setAutoCommit(defaultCommit);
    }2、修改CLOB对象(是在原CLOB对象基础上进行覆盖式的修改)public static void clobModify(String infile) throws Exception
    {
    /* 设定不自动提交 */
    boolean defaultCommit = conn.getAutoCommit();
    conn.setAutoCommit(false);try {
    /* 查询CLOB对象并锁定 */
    ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
    while (rs.next()) {
    /* 获取此CLOB对象 */
    oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
    /* 进行覆盖式修改 */
    BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
    BufferedReader in = new BufferedReader(new FileReader(infile));
    int c;
    while ((c=in.read())!=-1) {
    out.write(c);
    }
    in.close();
    out.close();
    }
    /* 正式提交 */
    conn.commit();
    } catch (Exception ex) {
    /* 出错回滚 */
    conn.rollback();
    throw ex;
    }/* 恢复原提交状态 */
    conn.setAutoCommit(defaultCommit);
    } 3、替换CLOB对象(将原CLOB对象清除,换成一个全新的CLOB对象)public static void clobReplace(String infile) throws Exception
    {
    /* 设定不自动提交 */
    boolean defaultCommit = conn.getAutoCommit();
    conn.setAutoCommit(false);try {
    /* 清空原CLOB对象 */
    stmt.executeUpdate("UPDATE TEST_CLOB SET CLOBCOL=EMPTY_CLOB() WHERE ID='111'");
    /* 查询CLOB对象并锁定 */
    ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
    while (rs.next()) {
    /* 获取此CLOB对象 */
    oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
    /* 更新数据 */
    BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
    BufferedReader in = new BufferedReader(new FileReader(infile));
    int c;
    while ((c=in.read())!=-1) {
    out.write(c);
    }
    in.close();
    out.close();
    }
    /* 正式提交 */
    conn.commit();
    } catch (Exception ex) {
    /* 出错回滚 */
    conn.rollback();
    throw ex;
    }/* 恢复原提交状态 */
    conn.setAutoCommit(defaultCommit);
    }4、CLOB对象读取public static void clobRead(String outfile) throws Exception
    {
    /* 设定不自动提交 */
    boolean defaultCommit = conn.getAutoCommit();
    conn.setAutoCommit(false);try {
    /* 查询CLOB对象 */
    ResultSet rs = stmt.executeQuery("SELECT * FROM TEST_CLOB WHERE ID='111'");
    while (rs.next()) {
    /* 获取CLOB对象 */
    oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
    /* 以字符形式输出 */
    BufferedReader in = new BufferedReader(clob.getCharacterStream());
    BufferedWriter out = new BufferedWriter(new FileWriter(outfile));
    int c;
    while ((c=in.read())!=-1) {
    out.write(c);
    }
    out.close();
    in.close();
    }
    } catch (Exception ex) {
    conn.rollback();
    throw ex;
    }/* 恢复原提交状态 */
    conn.setAutoCommit(defaultCommit);
    }
      

  2.   

    public  String clobsubread(String table,String wherestr,String field){
         String instr="";
         // DbConn dbCon = new DbConn();
        try {         //conn= dbCon.getConnection();
             /* 查询此CLOB对象并锁定 */
             stm=conn.createStatement();
             sql="SELECT "+field+" FROM "+table+" "+wherestr+" FOR UPDATE";
             System.out.println("clobsql:"+sql);
             rs = stm.executeQuery(sql);
             //stm.executeUpdate(this.sql);         while (rs.next()) {          /* 取出此CLOB对象 */
              //weblogic.jdbc.vendor.oracle.OracleThinClob clob =            (weblogic.jdbc.vendor.oracle.OracleThinClob)rs.getClob(1);
                        java.sql.Clob clob = (java.sql.Clob)rs.getClob(1);
              StringWriter strw= new StringWriter();
             /* 以字符形式输出 */
             BufferedReader in = new BufferedReader(clob.getCharacterStream());
             BufferedWriter out = new BufferedWriter(strw);
            int c;
            while ((c=in.read())!=-1) {
              out.write(c);
             }
             out.write(instr);
             out.close();
             in.close();
           }       /* 正式提交 */       conn.commit();     } catch (Exception ex) {       /* 出错回滚 */
          try{
            conn.rollback();
          }catch(Exception e){}
          System.out.println(ex.getMessage());
          ex.printStackTrace();
        }
        System.out.println("instr:"+instr.toString());
        return instr;  }看看我的这段代码为什么不行????instr总是为""