Java SQL ResultSet Read getResultSetValue(ResultSet rs, int index)

Here you can find the source of getResultSetValue(ResultSet rs, int index)

Description

Retrieve a JDBC column value from a ResultSet, using the most appropriate value type.

License

Apache License

Parameter

Parameter Description
rs is the ResultSet holding the data
index is the column index

Exception

Parameter Description
SQLException if thrown by the JDBC API

Return

the value object

Declaration

public static Object getResultSetValue(ResultSet rs, int index) throws SQLException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.sql.*;

public class Main {
    /**/*from w w  w .  ja  v a 2  s  .c om*/
     * Retrieve a JDBC column value from a ResultSet, using the most appropriate
     * value type. The returned value should be a detached value object, not having
     * any ties to the active ResultSet: in particular, it should not be a Blob or
     * Clob object but rather a byte array respectively String representation.
     * <p>Uses the <code>getObject(index)</code> method, but includes additional "hacks"
     * to get around Oracle 10g returning a non-standard object for its TIMESTAMP
     * datatype and a <code>java.sql.Date</code> for DATE columns leaving out the
     * time portion: These columns will explicitly be extracted as standard
     * <code>java.sql.Timestamp</code> object.
     *
     * @param rs    is the ResultSet holding the data
     * @param index is the column index
     * @return the value object
     * @throws SQLException if thrown by the JDBC API
     * @see java.sql.Blob
     * @see java.sql.Clob
     * @see java.sql.Timestamp
     */
    public static Object getResultSetValue(ResultSet rs, int index) throws SQLException {
        Object obj = null;
        try {
            obj = rs.getObject(index);
        } catch (SQLException e) {
            if (e.getMessage().equals("The conversion from char to SMALLINT is unsupported.")) {
                //issue with sqlserver jdbc 3.0 http://social.msdn.microsoft.com/Forums/sqlserver/en-US/2c908b45-6f75-484a-a891-5e8206f8844f/conversion-error-in-the-jdbc-30-driver-when-accessing-metadata
                obj = rs.getString(index);
            } else {
                throw e;
            }
        }
        if (obj instanceof Blob) {
            obj = rs.getBytes(index);
        } else if (obj instanceof Clob) {
            obj = rs.getString(index);
        } else if (obj != null && obj.getClass().getName().startsWith("oracle.sql.TIMESTAMP")) {
            obj = rs.getTimestamp(index);
        } else if (obj != null && obj.getClass().getName().startsWith("oracle.sql.DATE")) {
            String metaDataClassName = rs.getMetaData().getColumnClassName(index);
            if ("java.sql.Timestamp".equals(metaDataClassName)
                    || "oracle.sql.TIMESTAMP".equals(metaDataClassName)) {
                obj = rs.getTimestamp(index);
            } else {
                obj = rs.getDate(index);
            }
        } else if (obj != null && obj instanceof java.sql.Date) {
            if ("java.sql.Timestamp".equals(rs.getMetaData().getColumnClassName(index))) {
                obj = rs.getTimestamp(index);
            }
        }
        return obj;
    }
}

Related

  1. getResultSetInstance(Connection conn, String sql)
  2. getResultSetMetaData(ResultSet rs)
  3. getResultSetRowString(ResultSet rs)
  4. getResultSetStrings(ResultSet rs)
  5. getResultSetValue(ResultSet rs, int index)
  6. getResultSetValue(ResultSet rs, int index, Class requiredType)
  7. getResultSetValue(ResultSet rs, int index, Class requiredType)
  8. getReturnCountInt(ResultSet rs, int column)
  9. getRow(ResultSet rs)