Categories
Java

JDBC: Finding a ResultSet column was null

Checking whether a value you received from a ResultSet is null is not as intuitive as you would think.

Share

Supposed you just executed a database call using JDBC and the results were stored in a ResultSet object called rs. Then, you would expect the following to work if you test whether you returned a null value:


if (rs.getString("field") == null) {
// do something
}

You would be wrong.
Instead, JDBC offers the oh so intuitive wasNull() method:

String val = rs.getString("field");
if (rs.wasNull())
{
// do something
}

The logic behind this is that the ResultSet object is not pre-fetched and hence we can not test for its value before JDBC actually retrieved the value. Am I right?…

This is based on a posting to the SQL Server 2000 JDBC newsgroup.

Share

Leave a Reply

Your email address will not be published. Required fields are marked *

 

Share