Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error about JDBC in O'Reilly's Enterprise Java in a Nutshell?

 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bosun Bello
Ranch Hand
Posts: 1511
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic