clob 轉 String

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;
}
}
相關文章
相關標籤/搜索