I have a Java SE 8 Spring 4.1.6-RELEASE application where I implement the org.springframework.jdbc.core.RowMapper<T> interface, and I had some questions about the java.sql.ResultSet interface, which is passed to T mapRow(ResultSet rs, int rowNum) .
When I check the ResultSet class, I see a bunch of methods to return the column values:
βββββββββββββββ¦βββββββββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββββββββββββββββββ ββββββββββββββββββββββββββββββββββββββββββββββββ β β β
β 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 general expectation / practice expected:
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 primitive types? Because everything else returns null for the SQL NULL instance SQL NULL .
If so, what is the point of having all the methods for different types when a typed Object method exists?
Also, what are the advantages / disadvantages of each approach?
Using getInt(String columnLabel) :
Integer resultingActionId = rs.getInt("RESULTING_ACTION_ID"); if (rs.wasNull) { resultingActionId = null }
Using getObject(String columnLabel) and pronunciation on Integer :
Integer resultingActionId = (Integer) rs.getObject("RESULTING_ACTION_ID");
Using getObject(String columnLabel, Class type) :
Integer resultingActionId = rs.getObject("RESULTING_ACTION_ID", Integer.class);
For example, I noticed that org.springframework.jdbc.core.JdbcTemplate had queryForLong , queryForInt , etc. methods to get a single value from a single query string and replaced them all in favor of the typed queryForObject method.
Thanks!