When working with a JDBC resultset I want to get Double instead of double since this column is nullable. Rs.getDouble returns 0.0 when the column is null.
You can check for wasNull
on your ResultSet to find out if the value was null
.
Note that you must first call one of the getter methods on a column to try to read its value and then call the method wasNull to see if the value read was SQL NULL.
If you really need a Double
afterwards, you can create it from the double
returned.
Use ResultSet.wasNull -> http://java.sun.com/javase/6/docs/api/java/sql/ResultSet.html#wasNull%28%29
Basically it works for all primitive types, you first use getDouble
, getInt
, etc., and then only if the result is 0 you use wasNull
.
An alternative to the aforementioned ResultSet#wasNull()
is to test ResultSet#getObject()
on null
so that you can nicely put it in a single line in combination with the ternary operator:
Double d = resultSet.getObject("column") != null ? resultSet.getDouble("column") : null;
instead of
Double d = resultSet.getDouble("column");
if (resultSet.wasNull()) {
d = null;
}
You could just do:
Double d = (Double) resultSet.getObject("column");
then you don't have to worry if it was null.