Java SQL ResultSet Clob Read getClobAsString(ResultSet rs, String name, String ifnull)

Here you can find the source of getClobAsString(ResultSet rs, String name, String ifnull)

Description

get Clob As String

License

Apache License

Declaration

public static String getClobAsString(ResultSet rs, String name, String ifnull) throws Exception 

Method Source Code

//package com.java2s;
/*/*from   w  w w. j a va  2  s .  com*/
 * Copyright (c) Jim Coles (jameskcoles@gmail.com) 2018 through present.
 *
 * Licensed under the following license agreement:
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Also see the LICENSE file in the repository root directory.
 */

import java.io.Reader;
import java.sql.*;

public class Main {
    public static String getClobAsString(ResultSet rs, String name, String ifnull) throws Exception {
        String strClob = null;
        Clob clob = rs.getClob(name);
        if (clob != null)
            strClob = clobToString(clob, ifnull);
        return strClob;
    }

    public static String clobToString(Clob clob, String ifnull) throws Exception {
        String retVal = null;
        if (clob != null) {
            long pos = 1L;
            int chunksize = 100; // totally arbitrary
            char aChars[] = new char[chunksize];
            StringBuilder sb = new StringBuilder(chunksize);
            Reader reader = clob.getCharacterStream();
            try {
                int num = reader.read(aChars);
                while (num > 0) {
                    sb.append(aChars, 0, num);
                    num = reader.read(aChars);
                }
            } finally {
                reader.close();
            }
            retVal = sb.toString();
        }
        retVal = ((retVal != null) ? retVal : ifnull);
        return retVal;
    }
}

Related

  1. getCLOBContent(ResultSet rs, int clobidx)
  2. getOracleClob(ResultSet p_rset, int p_col)
  3. readClobUTF16BinaryStream(ResultSet rs, String fieldName)