title: Java Spring - RowMapper ResultSet - Integer/null values
body: <p>I have a Java SE 8 Spring 4.1.6-RELEASE application, where I am implementing the <a href="http://docs.spring.io/spring/docs/4.1.6.RELEASE/javadoc-api/org/springframework/jdbc/core/RowMapper.html" rel="noreferrer"><code>org.springframework.jdbc.core.RowMapper&lt;T&gt;</code></a> interface, and I had some questions about the <a href="https://docs.oracle.com/javase/8/docs/api/java/sql/ResultSet.html" rel="noreferrer"><code>java.sql.ResultSet</code></a> interface that is passed in its <a href="http://docs.spring.io/spring/docs/4.1.6.RELEASE/javadoc-api/org/springframework/jdbc/core/RowMapper.html#mapRow-java.sql.ResultSet-int-" rel="noreferrer"><code>T mapRow(ResultSet rs, int rowNum)</code></a> method.</p>
  
  <p>When I inspect the <code>ResultSet</code> class, I see a bunch of methods to get column values back:</p>
  
  <pre>
  ╔══════════════╦═════════════════════╦════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╗
  ║ 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       ║
  ║ &lt;T&gt; T        ║ getObject           ║ an instance of type holding the column value                                                                                                               ║
  ╚══════════════╩═════════════════════╩════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════╝
  </pre>
  
  <p>Is the genernal expectation / practice to call:</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>etc., for all of the primitive types? As everything else returns <code>null</code> for the instance of <code>SQL NULL</code>.</p>
  
  <p>If so, what's the point of having all the methods for the different types when the typed Object method is there?</p>
  
  <p>Also, what are the pros/cons of each approach?</p>
  
  <ol>
  <li><p>Using <code>getInt(String columnLabel)</code>:</p>
  
  <p><pre><code>Integer resultingActionId = rs.getInt("RESULTING_ACTION_ID");
  if (rs.wasNull) {
      resultingActionId = null
  }</pre></code></p></li>
  <li><p>Using <code>getObject(String columnLabel)</code> and casting to <code>Integer</code>:</p>
  
  <p><pre><code>Integer resultingActionId = (Integer) rs.getObject("RESULTING_ACTION_ID");</pre></code></p></li>
  <li><p>Using <code>getObject(String columnLabel, Class type)</code>:</p>
  
  <p><pre><code>Integer resultingActionId = rs.getObject("RESULTING_ACTION_ID", Integer.class);</pre></code></p></li>
  </ol>
  
  <p>For instance, I noticed the <a href="http://docs.spring.io/spring/docs/4.1.6.RELEASE/javadoc-api/org/springframework/jdbc/core/JdbcTemplate.html" rel="noreferrer"><code>org.springframework.jdbc.core.JdbcTemplate</code></a> used to have <s><code>queryForLong</code></s>, <s><code>queryForInt</code></s>, etc. methods for getting a single value from a single row query and replaced them all in favor of a typed <code>queryForObject</code> method.</p>
  
  <p>Thanks!</p>