• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

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?
0
william007
Asked:
william007
  • 5
  • 3
  • 2
  • +4
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 5
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now