File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes PreparedStatement and 'null' value in WHERE clause Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "PreparedStatement and Watch "PreparedStatement and New topic
Author

PreparedStatement and 'null' value in WHERE clause

Radoslaw Sztando
Ranch Hand

Joined: Mar 11, 2004
Posts: 40
Hi,



Why above code does not update column AGE for all records which NAME is null? In normal query I would rather use "...WHERE NAME IS NULL" for this case but I want to have general solution (handling both NULL and not NULL values in WHERE clause). Guys, do you have any idea how to do it?


Regards,
Radek Sztando
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26184
    
  66

Originally posted by Radoslaw Sztando:
Why above code does not update column AGE for all records which NAME is null?

Because you aren't using "is null". Null will never match on =.


but I want to have general solution (handling both NULL and not NULL values in WHERE clause). Guys, do you have any idea how to do it?

You are on the right track with your if statement. You just need to add a bit more logic:



[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Xiangning Liu
Greenhorn

Joined: Dec 20, 2005
Posts: 3
Originally posted by Jeanne Boyarsky:



In this case, it's not necessary to use PreparedStatement, why not use Statement directly?

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 26184
    
  66

Originally posted by Xiangning Liu:
In this case, it's not necessary to use PreparedStatement, why not use Statement directly?

Because prepared statements protect you from SQL injection. I'd much rather the JDBC driver handle that than my having to write a (possibly incorrect) validator.
Srilakshmi Vara
Ranch Hand

Joined: Jul 21, 2004
Posts: 169
In your query your are checking

name = null or name = passed value

in oracle name = null is different from name is null.

Change your code accordingly.

HTH
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: PreparedStatement and 'null' value in WHERE clause
 
Similar Threads
Cannot perform SQL UPDATE null
How to deal with single quote?
storing BLOB type in oracle db
JDBC issue with Oracle LONG datatype
Html Input to oracle through servlet and JSP output