clob To String Safe - Java JDBC

Java examples for JDBC:Binary Data

Description

clob To String Safe

Demo Code


import org.apache.log4j.Logger;
import javax.sql.rowset.serial.SerialClob;
import java.io.IOException;
import java.io.Reader;
import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Main{
    private static final Logger log = Logger.getLogger(JDBCUtil.class);
    public static String clobToStringSafe(Clob clob) throws IOException,
            SQLException {/*from w w w .j  a v a  2 s  .  c  o m*/
        try {
            return clobToString(clob);
        } catch (Exception e) {
            log.error("clobToStringSafe: exc=" + e.getMessage(), e);
            e.printStackTrace();
            return null;
        }
    }
    public static String clobToString(Clob clob) throws IOException,
            SQLException {
        if (clob == null)
            return null;
        Reader reader = clob.getCharacterStream();
        int c;
        StringBuilder sb = new StringBuilder();
        while ((c = reader.read()) != -1) {
            sb.append(((char) c));
        }
        return sb.toString();
    }
}

Related Tutorials