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 how to check for null string in resultset Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to check for null string in resultset" Watch "how to check for null string in resultset" New topic
Author

how to check for null string in resultset

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30789
    
157

Someone e-mailed me this question. Normally I just say to use the forums. But I really want to answer this one. So posting the question here.

I was hoping you could help me with a question I had in Java. So I'm
using a Result Set to get a result. However, one of my strings has a
null value. I've tried:

If(rs.getString("gender")==0)

If(rs.getString("gender")==null)

If(rs.getString("gender").equals(""))

If(rs.getString("gender").matches(""))

but none of them work. Can you please tell me how to check if the
'gender' field is null or not?


[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
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30789
    
157

Call rs.wasNull() right after calling rs.getString(). It returns a boolean telling you if the last value returned was a database null.
Scott Selikoff
author
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3712
    
    5

If it's a string, ==null should work. Is it possible you're storing an empty string instead of null in your database? Try running a query with 'WHERE gender IS NULL' in the database and see if it returns any records.


My Blog: Down Home Country Coding with Scott Selikoff
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to check for null string in resultset