How do I in JDBC read a possibly null double value from resultSet?

Depending on your JDBC driver and database, you may be able to use a boxed type and cast:

Double doubleValueOrNull = (Double)rs.getObject(1); // or .getObject("columnName")

It will be null if the column was NULL.

Be careful to check this still works if you change database.


Option 1 is closest:

double d = rs.getDouble(1);
if (rs.wasNull()) {
  // do something
} else {
  // use d
}

It's not very nice, but that's JDBC. If the column was null, the double value is considered "bad", so you should check using wasNull() every time you read a primitive that is nullable in the database.


Use:

rs.getObject(1)==null?null:rs.getBigDecimal(1).doubleValue()