aspose 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
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: 30929
    
158

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: 30929
    
158

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: 3716
    
    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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to check for null string in resultset