Getting Integer object from ResultSet [duplicate]

Just check if the field is null or not using ResultSet#getObject().

Integer foo = resultSet.getObject("foo") != null ? resultSet.getInt("foo") : null;

Or, if you can guarantee that you use the right DB column type so that ResultSet#getObject() really returns an Integer (and thus not Long, Short or Byte), then you can also just typecast it.

Integer foo = (Integer) resultSet.getObject("foo");

UPDATE: For Java 1.7+

Integer foo = resultSet.getObject("foo", Integer.class);

Leave a Comment