PreparedStatement setNull (..)

Java PreparedStatement provides the ability to explicitly set the value to Null. This feature:

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

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

 prepStmt.setString(null) 

?

+61
java jdbc prepared-statement
Aug 31 '09 at 13:18
source share
4 answers

This guide says:

6.1.5 Sending JDBC NULL as an IN Parameter

The setNull method allows the programmer to send the JDBC NULL value (total NULL value) for the database as an IN parameter. Note, however, that you still need to specify the type of the JDBC parameter.

JDBC NULL will also be sent to the database when the Java null value is passed to the setXXX method (if it takes Java objects as arguments). However, the setObject method can only be null if a JDBC type is specified.

+52
Aug 31 '09 at 13:23
source share

but watch this ....

 Long nullLong = null; preparedStatement.setLong( nullLong ); 

- disables null pointer exception -

because the prototype

 setLong( long ) 

NOT

 setLong( Long ) 

Glorious to catch you.

+57
Mar 03 2018-12-12T00:
source share

Finally, I did a little test, and although I programmed it, it occurred to me that without the setNull (..) method, there would be no way to set null values ​​for Java primitives. For objects in both directions

 setNull(..) 

and

 set<ClassName>(.., null)) 

behave the same way.

+11
Sep 01 '09 at 11:10
source share

You may also consider using preparedStatement.setObject(index,value,type);

+3
Nov 24 '15 at 3:59
source share



All Articles