Showing posts with label java jdbc wasNull callablestatement. Show all posts
Showing posts with label java jdbc wasNull callablestatement. Show all posts

Saturday, March 28, 2009

Check wasNull with JDBC CallableStatement

Suppose you have a CallableStatement which calls a database function and returns an integer. You would register the out parameter by
cs.registerOutParameter(1, Types.Integer);
cs.execute();

Let's say you want to check if NULL is returned as below.
Integer result = cs.getInt(1);
if (result == null) {
// NULL
}

This is wrong because result will be 0 (zero), instead of null, if NULL is returned from the database function. This is documented here.

The right way to check is to use wasNull.
Integer result = cs.getInt(1);
if (cs.wasNull) {
// NULL
}