Get a long string from ResultSet, which could be a TEXT or CLOB type. - Java java.sql

Java examples for java.sql:Clob

Description

Get a long string from ResultSet, which could be a TEXT or CLOB type.

Demo Code


//package com.java2s;
import java.sql.Clob;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Main {
    /**// w  ww.  ja  v a2s .c o m
     * Get a long string, which could be a TEXT or CLOB type.
     * (CLOBs require special handling -- this method normalizes the reading of them)
     */
    public static String getLongString(ResultSet rs, int pos)
            throws SQLException {
        String s = rs.getString(pos);
        if (s != null) {
            // It's a String-based datatype, so just return it.
            return s;
        } else {
            // It may be a CLOB.  If so, return the contents as a String.
            try {
                Clob c = rs.getClob(pos);
                return c.getSubString(1, (int) c.length());
            } catch (Throwable th) {
                th.printStackTrace();
                return null;
            }
        }
    }
}

Related Tutorials