Here you can find the source of getResultSetValue(ResultSet rs, int index)
Parameter | Description |
---|---|
rs | is the ResultSet holding the data |
index | is the column index |
Parameter | Description |
---|---|
SQLException | if thrown by the JDBC API |
public static Object getResultSetValue(ResultSet rs, int index) throws SQLException
//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; } }