1. Use the dbms_lob.substr() method to convert the corresponding fields into strings as follows:
- select dbms_lob.substr(content) from NEWS
This method has a disadvantage. When the length of the content field exceeds a certain value, an error will be reported.
2. Get the Clob object, and get the field content by stream processing in Java, there is no length limit in this method
- select content from NEWS
- // Convert word CLOB to STRING type
- public String ClobToString(Clob clob) throws SQLException, IOException {
- String reString = "";
- java.io.Reader is = clob .getCharacterStream();// Get the stream
- BufferedReader br = new BufferedReader(is);
- String s = br.readLine();
- StringBuffer sb = new StringBuffer();
- while (s != null) {// Execute the loop to take out all the strings and pay them to StringBuffer from StringBuffer to STRING
- sb.append(s);
- s = br.readLine();
- }
- reString = sb.toString();
- return reString;
- }
The next point is to convert the object of the field obtained from the database into a Clob object, as follows:
- String content = ClobToString((Clob)obj[1]);
Among them, my obj is the field array obtained from the database, and obj[1] corresponds to the Clob object
String s1=
"走上编程不归路……"
;
Clob c =
new
SerialClob(s1.toCharArray());
//String 转 clob
Blob b =
new
SerialBlob(s1.getBytes(
"GBK"
));
//String 转 blob
// 也可以这样不传字符集名称,默认使用系统的
// Blob b = new SerialBlob(s1.getBytes());
String clobString = c.getSubString(
1
, (
int
) c.length());
//clob 转 String
String blobString =
new
String(b.getBytes(
1
, (
int
) b.length()),
"GBK"
);
//blob 转 String
// 前面若没传入字符集名称,则这里也不需要传入,以免出错
// String blobString = new String(b.getBytes(1, (int) b.length()));
System.out.println(clobString);
System.out.println(blobString);
// Convert word CLOB to STRING type - String content = ClobToString((Clob)obj[1]);
- public String ClobToString(Clob clob) throws SQLException, IOException {
- String reString = "";
- java.io.Reader is = clob.getCharacterStream();// 得到流
- BufferedReader br = new BufferedReader(is);
- String s = br.readLine();
- StringBuffer sb = new StringBuffer();
- while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由 StringBuffer转成STRING
- sb.append(s);
- s = br.readLine();
- }
- reString = sb.toString();
- return reString;
- }
http://www.oschina.net/code/snippet_135225_5485