A ResultSet
provides method getInt()
that returns primitive int
. Is it possible to obtain the Integer
object, which permits null
? The DB field I'm retrieving is nullable and getInt()
returns me 0
whenever the field is null
.
Thanks
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With