PreparedStatement setNull(..)
You could also consider using preparedStatement.setObject(index,value,type);
This guide says:
6.1.5 Sending JDBC NULL as an IN parameter
The
setNull
method allows a programmer to send a JDBCNULL
(a generic SQLNULL
) value to the database as an IN parameter. Note, however, that one must still specify the JDBC type of the parameter.A JDBC
NULL
will also be sent to the database when a Javanull
value is passed to asetXXX
method (if it takes Java objects as arguments). The methodsetObject
, however, can take anull
value only if the JDBC type is specified.
So yes they're equivalent.
Finally I did a small test and while I was programming it it came to my mind, that without the setNull(..) method there would be no way to set null values for the Java primitives. For Objects both ways
setNull(..)
and
set<ClassName>(.., null))
behave the same way.
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.