• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to check for null string in resultset

 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36026
430
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
Sheriff
Posts: 36026
430
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
Bartender
Posts: 4033
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.
 
Can't .... do .... plaid .... So I did this tiny ad instead:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!