Java SQL ResultSet Read getBigObject(ResultSet set, int columnIndex)

Here you can find the source of getBigObject(ResultSet set, int columnIndex)

Description

Get Object by special column index

License

Open Source License

Parameter

Parameter Description
set a parameter
columnIndex a parameter

Exception

Parameter Description
SQLException an exception
IOException an exception

Declaration

public static Object getBigObject(ResultSet set, int columnIndex) throws SQLException 

Method Source Code


//package com.java2s;
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.ResultSet;

import java.sql.SQLException;

public class Main {
    private static final String NULLDATE = "0000-00-00 00:00:00";

    /**//from   w  w w . j  av  a2  s  .c  o m
     * 
     * Get Object by special column index
     * 
     * @param set
     * @param columnIndex
     * @return
     * @throws SQLException
     * @throws IOException
     */
    public static Object getBigObject(ResultSet set, int columnIndex) throws SQLException {
        Object object = null;
        try {
            object = set.getObject(columnIndex);
            if (object != null && object instanceof Clob) {
                Reader is = ((Clob) object).getCharacterStream();
                BufferedReader br = new BufferedReader(is);
                String str = br.readLine();
                StringBuffer sb = new StringBuffer();
                while (str != null) {
                    sb.append(str);
                    str = br.readLine();
                }
                return sb.toString();
            }
        } catch (SQLException e) {
            if (NULLDATE.equals(set.getString(columnIndex))) {
                object = null;
            } else {
                throw e;
            }

        } catch (IOException e) {
            object = null;
        }
        return object;
    }

    /**
     * 
     * Get Object by special column index
     * 
     * @param set
     * @param columnIndex
     * @return
     * @throws SQLException
     * @throws IOException
     */
    public static Object getBigObject(ResultSet set, String columnName) throws SQLException {
        Object object = null;
        try {
            object = set.getObject(columnName);
            if (object != null && object instanceof Clob) {
                Reader is = ((Clob) object).getCharacterStream();
                BufferedReader br = new BufferedReader(is);
                String str = br.readLine();
                StringBuffer sb = new StringBuffer();
                while (str != null) {
                    sb.append(str);
                    str = br.readLine();
                }
                return sb.toString();
            }
        } catch (SQLException e) {
            if (NULLDATE.equals(set.getString(columnName))) {
                object = null;
            } else {
                throw e;
            }

        } catch (IOException e) {
            object = null;
        }
        return object;
    }

    /**
     * 
     * Get Object by special column index
     * 
     * @param set
     * @param columnIndex
     * @return
     * @throws SQLException
     */
    public static Object getObject(ResultSet set, int columnIndex) throws SQLException {
        Object object = null;
        try {
            object = set.getObject(columnIndex);
        } catch (SQLException e) {
            if (NULLDATE.equals(set.getString(columnIndex))) {
                object = null;
            } else {
                throw e;
            }

        }
        return object;
    }

    /**
     * 
     * Get Object by special column name
     * 
     * @param set
     * @param columnName
     * @return
     * @throws SQLException
     */
    public static Object getObject(ResultSet set, String columnName) throws SQLException {
        Object object = null;
        try {
            object = set.getObject(columnName);
        } catch (SQLException e) {
            if (NULLDATE.equals(set.getString(columnName))) {
                object = null;
            } else {
                throw e;
            }

        }
        return object;
    }
}

Related

  1. getArrayFromResultSet(ResultSet rs, String field)
  2. getAsString(ResultSet res, String field)
  3. getBestRowId(String schema, String tableName, int scope, String nullable, ResultSet[] rs)
  4. getBigInteger(ResultSet resultSet, int columnIndex)
  5. getBigIntegerFromResultSet(ResultSet rs, String db_name)
  6. getCalendar(ResultSet resultSet, String columnName)
  7. getCharacterStream(ResultSet resultSet, String columnName)
  8. getChunkDelimiters(ResultSet rs, int tsColumnIndex, int chunkSize)
  9. getClassName(ResultSetMetaData meta, int index)