How to Handle SQL Null Values


  • Java primitive types cannot have null values.
  • Do not use a primitive type when your query might return a SQL null.
  • Use ResultSet.wasNull() to determine whether a column has a null value.
while(rs.next())
{
String name = rs.getString("NAME");
if(rs,wasNull)
{
// Handle Null values.
}
}

Handling SQL Null Values
  • If your query could return a SQL null, you should use a getXXX() method that returns an object type, not one that returns a Java primitive.
  • The example on the slide shows how to use ResultSet.wasNull() to find out whether a column has a null value. Note that you must call getXXX() to read the column’s value before you can find out whether it has a null value.

Search: