第一种:
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());
本文介绍了两种将数据库中Clob类型的数据转换为String的方法。第一种使用了BufferedInputStream进行读取并转换;第二种直接通过getSubString方法获取内容,解决了中文乱码问题。
2491

被折叠的 条评论
为什么被折叠?



