Most efficient solution for reading CLOB to String, and String to CLOB in Java?

前端 未结 11 1258
长发绾君心
长发绾君心 2020-12-01 07:04

I have a big CLOB (more than 32kB) that I want to read to a String, using StringBuilder. How do I do this in the most efficient way? I can not use the \"int length\" constru

11条回答
  •  既然无缘
    2020-12-01 07:47

    public static String readClob(Clob clob) throws SQLException, IOException {
        StringBuilder sb = new StringBuilder((int) clob.length());
        Reader r = clob.getCharacterStream();
        char[] cbuf = new char[2048];
        int n;
        while ((n = r.read(cbuf, 0, cbuf.length)) != -1) {
            sb.append(cbuf, 0, n);
        }
        return sb.toString();
    }
    

    The above approach is also very efficient.

提交回复
热议问题