aspose file tools
The moose likes JDBC and Relational Databases 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 The Software Craftsman this week in the Agile forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
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: 31668
    
189

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?


[OCA 8 book] [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: 31668
    
189

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: 3740
    
  10

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.


[OCA 8 Book] [Blog]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to check for null string in resultset