• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to check for null string in resultset

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34669
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34669
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4014
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic