0

What is the uses of setNull() method in PreparedStatement interface? I looked in this post.

It says: Without the setNull(..) method there would be no way to set null values for the Java primitives.

however with autoboxing in JDK5, I think null values can be set on even primitive types.

There is another post in some other forum says:If you want to be portable to different databases, use the setNull() method.

However there is nothing clearly mentioned in Java doc. Could you help me understanding this?

Community
  • 1
  • 1
Vicky
  • 5,380
  • 18
  • 60
  • 83
  • "however with autoboxing in JDK5, I think null values can be set on even primitive types." - wrong. Autoboxing means you can assign primitive values to variables of object type; it doesn't let you pass null to a method which takes a primitive. – Tom Anderson Apr 30 '11 at 13:06

1 Answers1

1

I think it's easier to understand this if you view it from the database end. If you want to set a field to NULL in your database insert statement, then you need a way of telling the database that is should be set to NULL rather than the default value for the column. If in the database schema you have a nullable integer field, you would use set null to set it to the DB NULL value, rather than to its default value ( 0 ).

DaveH
  • 7,187
  • 5
  • 32
  • 53