Java中读取Oracle中Clob字段的两种方法

来源:互联网 发布:c语言 chm 编辑:程序博客网 时间:2024/06/06 13:13
第一种:
import java.io.BufferedInputStream;
import java.io.IOException;
import java.sql.Clob;
import java.sql.SQLException;
 
/**
 * 数据库处理对象
 *
 * @author 苏显斌
 *
 */
public final class DatabaseHelper {
       /**
        * 将Clob类型转换为String类型
        *
        * @author 苏显斌
        * @param clob
        *            存放内容的变量
        * @return 返回Clob类型的String类型内容.
        */
       public static String changeClobToString(Clob clob) throws IOException,
                     SQLException {
              BufferedInputStream bi = new BufferedInputStream(clob.getAsciiStream());
              int len = (int) clob.length();
              byte[] by = new byte[len];
              int i;
              while (-1 != (i = bi.read(by, 0, by.length))) {
                     bi.read(by, 0, i);
              }
              String clobValue = new String(by);
              bi.close();
 
              return clobValue;
       }
}

第二种(可消除中文乱码):

Clob clob = details[i].getHelpBody();
String content = clob.getSubString((long)1,(int)clob.length());

原创粉丝点击