aspose file tools*
The moose likes JDBC and the fly likes MySQL Prepared Statement & NULL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "MySQL Prepared Statement & NULL" Watch "MySQL Prepared Statement & NULL" New topic
Author

MySQL Prepared Statement & NULL

Rob J Ford
Greenhorn

Joined: Oct 30, 2001
Posts: 3
I have an app with a CachedRowSetImpl that reads from a MySQL database (using "com.mysql.jdbc.Driver")

I'm passing the RowSet a prepared statement that looks like this:



...and then setting the parameter with setString, setInt, etc... I'm using the LIKE operater to allow me to enter the '%' wildcard into the parameter.

The only place where its falling down is when I try to select NULL fields. Using setNULL does not work as MySQL treats NULL differently from other databases. The usual way to execute a clause on NULL is



...but using the 'LIKE' or '=' operators fails when used with NULL under MySQL.

So is there any way I can put a WHERE clause on a column with a NULL value using a PreparedStatement like I have above?
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30516
    
150

Rob J Ford wrote:...but using the 'LIKE' or '=' operators fails when used with NULL under MySQL.

So is there any way I can put a WHERE clause on a column with a NULL value using a PreparedStatement like I have above?

It should fail. Testing for equality (or like) is undefined according to the SQL starndard. MySql is behaving correctly. You have to use "is null" to check for null in SQL.


[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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MySQL Prepared Statement & NULL