Clob字段的处理

Clob字段的处理

Clob转换为String

Clob转换为String的三种方法:

  1. 使用Clob对象的getSubString(long , int)方法
  2. 使用字节流读取
  3. 使用字符流读取

代码如下:

 			Clob clob = resultSet.getClob(1);

            //第一种转换方法 使用Clob的getSubString(long , int)方法
            if(clob!= null){
                String clobString = clob.getSubString(1L,(int)clob.length());
                System.out.println(clobString);
            }

            //第二种转换方法,使用字节流读取
            if(clob!= null){
                InputStream input = clob.getAsciiStream();
                int length = (int)clob.length();
                String str = "";
                byte [] bb = new byte[length];
                try {
                    int result = input.read(bb);
                    if(result!= -1)
                        str = new String(bb);
                    System.out.println(str);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            //第三种方法,使用字符流读取
            if(clob != null){
                Reader reader = clob.getCharacterStream();
                BufferedReader bufferedReader = new BufferedReader(reader);
                String result = "";
                String str = "";
                try {
                    while((str = bufferedReader.readLine()) != null){
                        result += (str + "\n");
                    }
                    System.out.println(result);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

把字符串转为java.sql.Clob对象

Clob clob2  = new javax.sql.rowset.serial.SerialClob(result.toCharArray());

把字符串转为oracle.sql.Clob对象

String str = "I am a clob value";
oracle.sql.CLOB clob = oracle.sql.CLOB.createTemporary(conn, false,oracle.sql.CLOB.DURATION_SESSION);
clob.open(oracle.sql.CLOB.MODE_READWRITE);
writer = clob.getCharacterOutputStream();
writer.write(str);
writer.flush();
writer.close();

备注:需要ojdbc4.jar,如果针对于jdk1.6的话则最好是用ojdbc6.jar

插入一个clob类型的字段

clob字段不能直接插入
插入clob类型的字段需要如下几个步骤

  1. 取消连接的自动提交
  2. 针对clob字段,插入一个占位符,即插入empty_clob
  3. 查询要记录,一定要是for update的
  4. 获取clob字段 ,把获取的java.sql.Clob强制转换为oracle.sql.Clob
  5. 获取oralce.sql.Clob对象的流
  6. 通过该流,把字符串写到数据库中取

代码如下(用的是dbutils):

插入的方法如下:

   	/**
 	* 数据库中插入Clob类型字段
 	*/
public void insertClob(){
    String sql = "insert into esb.esb_exception_manage_ (id_  , app_name , flow_name , root_  , detail_ , status_code_) values('218949957478440256' , 'app_name' , 'flow_name' , 'root' , empty_clob() ,1)";
    QueryRunner qr = new QueryRunner();
    getConnection();

    try {
        conn.setAutoCommit(false);
        int result = qr.update(conn , sql);          
        String sql_clob = "select * from esb.esb_exception_manage_ where id_ = ? for update ";
        ResultSetClobHandle resultSetClobHandle = new ResultSetClobHandle();
        int result2 = qr.query(conn , sql_clob , resultSetClobHandle , "258949957478240256");
        conn.commit();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}

ResultSetHandle的实现类如下:

    private class ResultSetClobHandle implements  ResultSetHandler<Integer>{
    @Override
    public Integer handle(ResultSet resultSet) throws SQLException {
        if(resultSet.next()){
            oracle.sql.CLOB clob = (oracle.sql.CLOB)resultSet.getClob("detail_");
            Writer writer = clob.getCharacterOutputStream();  // 获取Clob对象的流
            String str = "it is a  big string!";
            try {
                writer.write(str);   //把字符串写入到数据库的clob对象中
                writer.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return 1;
    }
}
posted @ 2017-12-14 18:26  别离的岁月  阅读(763)  评论(0编辑  收藏  举报