PreparedStatement setNull(..)

paweloque picture paweloque · Aug 31, 2009 · Viewed 142.2k times · Source

Java PreparedStatement provides a possibility to explicitely set a Null value. This possibility is:

prepStmt.setNull(parameterIndex, Types.VARCHAR);

Are the semantics of this call the same as when using a specific setType with a null parameter?

prepStmt.setString(null);

?

Answer

Owen picture Owen · Mar 3, 2012

but watch out for this....

Long nullLong = null;

preparedStatement.setLong( nullLong );

-thows null pointer exception-

because the protype is

setLong( long )   

NOT

setLong( Long )

nice one to catch you out eh.