I was trying to help someone with a JDBC question about how to set a null value in a table when I came upon something in the Java Enterprise in a Nutshell book. On the bottom of page 28, the text reads:
You can use PreparedStatement to insert null values into a database, either by calling the setNull method or by passing a null value to one of the setXXX() methods that take an Object. In either case you must specify the target SQL type. Let's clarify with an example. We want to set the first parameter of a prepared statement to the value of an Integer object, while the second parameter, which is a VARCHAR, should be null. Here's some code that does that:
I'm able to get the line that is commented out (the setNull) to work, but the line that sets a null value using a setObject call gives me a NullPointerException. Here's the code I'm using:
As you can see, my line to set the null looks just like the one in the book. Is this an error in the text or am I missing something? In case it's important, I'm currently using a MS Access database (although that shouldn't matter, I don't think). Thanks, Corey
Are you saying it's not compiling with setNull()? If that's the case, this method is available with JDK 1.2 and above. You must also be using JDBC2.0 and above.
------------------ Bosun SCJP for the Java� 2 Platform
Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Joined: Dec 20, 2001
Are you saying it's not compiling with setNull()?
No, no - It actually compiles just fine no matter which method I use (either setNull or setObject). When I use setNull, everything works just fine. When I use setObject and supply null as a parameter, however, I get a NullPointerException at run-time. The book, however, claims that this should work. Any ideas? Corey
subject: Error about JDBC in O'Reilly's Enterprise Java in a Nutshell?