views:

561

answers:

2

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

prepStmt.setNull(<n>, Types.VARCHAR)

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

prepStmt.setString(null)

?

+8  A: 

This guide says:

6.1.5 Sending JDBC NULL as an IN parameter

The setNull method allows a programmer to send a JDBC NULL (a generic SQL NULL) 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 Java null value is passed to a setXXX method (if it takes Java objects as arguments). The method setObject, however, can take a null value only if the JDBC type is specified.

djna
+1: Interesting. I assumed that's how setXXX worked with nulls, but I'd never actually tested it or read the docs for it.
R. Bemrose
A: 

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.

lewap