I have a Java SE 8 Spring 4.1.6-RELEASE application, where I am implementing the org.springframework.jdbc.core.RowMapper<T>
interface, and I had some questions about the java.sql.ResultSet
interface that is passed in its T mapRow(ResultSet rs, int rowNum)
method.
When I inspect the ResultSet
class, I see a bunch of methods to get column values back:
╔══════════════╦═════════════════════╦════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗ ║ Return Type ║ Method ║ Return (javadoc, se 8) ║ ╠══════════════╬═════════════════════╬════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╣ ║ String ║ getString ║ the column value; if the value is SQL NULL, the value returned is null ║ ║ boolean ║ getBoolean ║ the column value; if the value is SQL NULL, the value returned is false ║ ║ byte ║ getByte ║ the column value; if the value is SQL NULL, the value returned is 0 ║ ║ short ║ getShort ║ the column value; if the value is SQL NULL, the value returned is 0 ║ ║ int ║ getInt ║ the column value; if the value is SQL NULL, the value returned is 0 ║ ║ long ║ getLong ║ the column value; if the value is SQL NULL, the value returned is 0 ║ ║ float ║ getFloat ║ the column value; if the value is SQL NULL, the value returned is 0 ║ ║ double ║ getDouble ║ the column value; if the value is SQL NULL, the value returned is 0 ║ ║ BigDecimal ║ getBigDecimal ║ the column value; if the value is SQL NULL, the value returned is null ║ ║ byte[] ║ getBytes ║ the column value; if the value is SQL NULL, the value returned is null ║ ║ Date ║ getDate ║ the column value; if the value is SQL NULL, the value returned is null ║ ║ Time ║ getTime ║ the column value; if the value is SQL NULL, the value returned is null ║ ║ Timestamp ║ getTimestamp ║ the column value; if the value is SQL NULL, the value returned is null ║ ║ InputStream ║ getAsciiStream ║ a Java input stream that delivers the database column value as a stream of one-byte ASCII characters; if the value is SQL NULL, the value returned is null ║ ║ Reader ║ getCharacterStream ║ a java.io.Reader object that contains the column value; if the value is SQL NULL, the value returned is null in the Java programming language ║ ║ InputStream ║ getBinaryStream ║ a Java input stream that delivers the database column value as a stream of uninterpreted bytes; if the value is SQL NULL, the value returned is null ║ ║ <T> T ║ getObject ║ an instance of type holding the column value ║ ╚══════════════╩═════════════════════╩════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝
Is the genernal expectation / practice to call:
rs.getObject("COLUMN_NAME", Boolean.class);
rs.getObject("COLUMN_NAME", Byte.class);
rs.getObject("COLUMN_NAME", Short.class);
rs.getObject("COLUMN_NAME", Integer.class);
rs.getObject("COLUMN_NAME", Long.class);
etc., for all of the primitive types? As everything else returns null
for the instance of SQL NULL
.
If so, what's the point of having all the methods for the different types when the typed Object method is there?
Also, what are the pros/cons of each approach?
Using
getInt(String columnLabel)
:Integer resultingActionId = rs.getInt("RESULTING_ACTION_ID"); if (rs.wasNull) { resultingActionId = null }
Using
getObject(String columnLabel)
and casting toInteger
:Integer resultingActionId = (Integer) rs.getObject("RESULTING_ACTION_ID");
Using
getObject(String columnLabel, Class type)
:Integer resultingActionId = rs.getObject("RESULTING_ACTION_ID", Integer.class);
For instance, I noticed the org.springframework.jdbc.core.JdbcTemplate
used to have , queryForLong
, etc. methods for getting a single value from a single row query and replaced them all in favor of a typed queryForInt
queryForObject
method.
Thanks!