Reputation: 661
In Java, is there a universal way to check NULL in a ResultSet (java.sql.ResultSet)?
The ResultSet interface does not have a direct method to check NULL. The closest thing is the wasNull() method. So, I have come up with this idea:
ResultSet rs; //the ResultSet object
Object theValue;
boolean isValueNull;
theValue = rs.getObject("MY_COLUMN");
isValueNull = rs.wasNull();
After executing the code, isValueNull should indicate whether the value is NULL.
But, there is a question: Does rs.getObject(...) work for all columns? By contrast, if you call rs.getBigDecimal(...) on a string column, an exception may occur. I guess that rs.getObject(...) works for all columns, but I am not sure.
Upvotes: 1
Views: 477
Reputation: 6412
https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html
The universal way to check if last value from resultSet was null is to check rs.wasNull();
Yes, wasNull indicates whether the value was NULL.
Yes, getObject works on all columns.
Upvotes: 1