clob 转 String

时间:2021-06-01 03:19:16
import javax.sql.rowset.serial.SerialClob;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.SQLException; class Clob2String {
public static void main(String[] args) throws Exception {
Clob clob = new SerialClob("*:zhe shi gu yi gou zao de zi fu chuan。".toCharArray());
Clob2String application = new Clob2String();
String res = application.clob2string(clob);
System.out.println(res);
String result = clob.getSubString(1, (int) clob.length());
System.out.println(result);
} public String clob2string(Clob clob) {
StringBuilder res = new StringBuilder();
String result = "";
try {
Reader reader = clob.getCharacterStream();
BufferedReader bufferedReader = new BufferedReader(reader);
int c;
while ((c = bufferedReader.read()) != -1) {
res.append((char)c);
}
result = res.toString();
} catch (IOException e) {
result = res.toString();
return result;
} catch (SQLException e) {
       result = res.toString();
     return result;
}
return result;
}
}