3.3.2 SQL NULL Versus Java null SQL and Java have a serious mismatch in handling null values. Specifically, using methods like getInt(), a Java ResultSet has no way of representing a SQL NULL value for any numeric SQL column. After retrieving a value from a ResultSet, it is therefore necessary to ask the ResultSet if the retrieved value represents a SQL NULL. For Java object types, a SQL NULL will often map to Java null. To avoid running into database oddities, however, it is recommended that you always check for SQL NULL. Checking for SQL NULL involves a single call to the wasNull( ) method in your ResultSet after you retrieve a value. The wasNull() method will return true if the last value read by a call to a getXXX() method was a SQL NULL. If, for example, your database allowed NULL values for PET_COUNT column because you do not know the number of pets of all your customers, a call togetInt() could return some driver attempt at representing NULL, most likely 0. So how do you know in Java who has pets and who has an unknown number of pets? A call to wasNull() will tell you if represents an actual in the database or a NULL value in the database.
SQL and Java have a serious mismatch in handling null values. Specifically, using methods like getInt(), a Java ResultSet has no way of representing a SQL NULL value for any numeric SQL column. After retrieving a value from a ResultSet, it is therefore necessary to ask the ResultSet if the retrieved value represents a SQL NULL. For Java object types, a SQL NULL will often map to Java null. To avoid running into database oddities, however, it is recommended that you always check for SQL NULL.
Checking for SQL NULL involves a single call to the wasNull( ) method in your ResultSet after you retrieve a value. The wasNull() method will return true if the last value read by a call to a getXXX() method was a SQL NULL. If, for example, your database allowed NULL values for PET_COUNT column because you do not know the number of pets of all your customers, a call togetInt() could return some driver attempt at representing NULL, most likely 0. So how do you know in Java who has pets and who has an unknown number of pets? A call to wasNull() will tell you if represents an actual in the database or a NULL value in the database.