How to check whether a field in a result set is a null value?

How to check whether a field in a result set is a null value?
LVL 9
william007Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mayank SAssociate Director - Product EngineeringCommented:
After you get the field:

String a = rs.getString ( "column name" ) ;

You can directly check if ( a == null )
0
Tommy BraasCommented:
What objects is referring to is:

int myIntegerFromTheDb = myResultSet.getInt();

if (myResultSet.wasNull()) {   // Checks to see if the last value read was null in the database
   // perform logic for null value
}

Columns of primitive type does not work for mayankeagle's example. The above will work for ALL column types.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Mayank SAssociate Director - Product EngineeringCommented:
>> Columns of primitive type does not work for mayankeagle's example

Yeah that is true.
0
VizyCommented:
If you wan't to check if your query has returned anything at all try moving cursor down to the first returned row.

if (rs.next ( )) {

}
0
Mayank SAssociate Director - Product EngineeringCommented:
Well, without doing that in the first place, you won't be able to scroll ahead and check for anything....
0
VizyCommented:
>> mayankeagle yeah i know .... was trying to say something more but accidentally posted the answer :) ... my bad ...first time user.

I use this when i COUNT() something and limit the output like this:

SELECT COUNT(*) FROM tbl_name GROUP BY col_name HAVING COUNT(*)>1;

If everything in my application works fine than this query should return an empty set. So if I want't my test to continue only if  this was true i do:

if (!rs.next ( )) {
  // things to do when no duplicates were found in my table
}

I know this is not exactly the answer for this question, but many beginners also wonder how to handle  above example. Of course this is not the only way.

0
rama_krishna580Commented:
Hi,

If you need to find out if the result set contains any records, I use the following method.

/** Determines if the Record Set is empty
* @param        none
* @return       boolean
*/
    public boolean isRecSetEmpty() {
        int m_origPos;
        boolean rtnValue = true;
 
        try {
            m_origPos = dbf.getRow();
 
            rtnValue = ( dbf.first() != true );
 
            /* put row back to where it was. */
            if (m_origPos > 0)  {
                dbf.absolute( m_origPos );
            }
 
        } catch (Exception e) {
            this.handleIOException(e);
        }
 
        return rtnValue;
 
    }

Where dbf is your resultset object.

An easier way might be to call .next() on your result set after it has just been created. It should return false if no records.

OR

use if(ResultSet.wasNull())

//checking if result set has any rows
ResultSet rs = ps.executeQuery();
if(rs.next()){
  // has at least one row
}else{
  //no rows
}

//checking if last column was null...
while(rs.next()){
  String a = rs.getString(1);
  if(rs.wasNull()){
    //last column was null
  }else{
    // last column was NOT null
  }
}


R.K
0
el_diosCommented:
Hi ,

wasNull() approach is the generally used one.

Cheers!
0
Mayank SAssociate Director - Product EngineeringCommented:
If you are using objects, then you can also check if ( str == null ) after str = rs.getString ( .... ) multiple times (because if call getString () or something else on the result-set after that, then wasNull () will not tell whether the last to last column was null or not)
0
william007Author Commented:
Thanks:-)
0
Tommy BraasCommented:
???
0
Mayank SAssociate Director - Product EngineeringCommented:
What was wrong in the accepted answer, orangehead :) ?
0
Tommy BraasCommented:
I just believe I added something to the answer, but I guess I'm wrong... :-|
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.