views:

701

answers:

6

In Java I'm trying to test for a null value, from a ResultSet, where the column is being cast to a primitive int type.

int iVal;
ResultSet rs = magicallyAppearingStmt.executeQuery(query);
if (rs.next()) {
  if (rs.getObject("ID_PARENT") != null && !rs.wasNull()) {
    iVal = rs.getInt("ID_PARENT");
  }
}

From the code fragment above, is there a better way to do this, and I assume that the second wasNull() test is redundant?

Educate us, and Thanks

+6  A: 

The default for ResultSet.getInt when the field value is NULL is to return 0, which is also the default value for your iVal declaration. In which case your test is completely redundant.

If you actually want to do something different if the field value is NULL, I suggest:

int iVal;
ResultSet rs = magicallyAppearingStmt.executeQuery(query);
if (rs.next()) {
    iVal = rs.getInt("ID_PARENT");
    if (rs.wasNull()) {
        // handle NULL field value
    }
}
Richard
`The default for ResultSet.getInt when the field value is NULL is to return 0` - can you add a proof-link? This looks a bit ridiculous. Just assume that the value is a foreign key and a record with '0'-id exists in a parent table.
Roman
@Richard: I've just found the same statement in docs. It worth a separate thread on SO imho. (http://java.sun.com/j2se/1.4.2/docs/api/java/sql/ResultSet.html#getObject(int))
Roman
@Roman - see the javadoc for getInt in ResultSet: "Returns: the column value; if the value is SQL NULL, the value returned is 0 "
Cowan
Good answer/comments, ty. Chris, below has an interesting hack as well.
ian_scho
A: 

I think, it is reduntant. rs.getObject("ID_PARENT") should return an Integer object or null, if the column value actually was NULL. So it should even be possible to do something like:

if (rs.next()) {
  Integer idParent = (Integer) rs.getObject("ID_PARENT");
  if (idParent != null) {
    iVal = idParent; // works for Java 1.5+
  } else {
    // handle this case
  }      
}
Andreas_D
A: 

AFAIK you can simply use

iVal = rs.getInt("ID_PARENT");
if (rs.wasNull()) {
  // do somthing interesting to handle this situation
}

even if it is NULL.

Peter Tillemans
A: 

Another nice way of checking, if you have control the SQL, is to add a default value in the query itseld for your int column. Then just check for that value.

e.g for ORACLE database, use NVL

SELECT NVL(ID_PARENT, -999) FROM TABLE_NAME;

then check

if (rs.getInt('ID_PARENT') != -999) { }

Of course this also is under the assumption that there is a value that wouldn't normally be found in the column

Chris
A: 

an other solution:

public class DaoTools
{
    static public Integer getInteger( ResultSet rs, String strColName ) throws SQLException
    {
        int nValue = rs.getInt( strColName );
        if ( rs.wasNull() ) return null;
        return new Integer( nValue );
    }
}
Patrice IMBERT
A: 

I myself use the following idiom (substitute -1 with whatever null-case value you want):

int i = rs.getObject("col") != null ? rs.getInt("col") : -1;

I find it personally prettier than:

int i = rs.getInt("col");
if (rs.wasNull()) {
    i = -1;
}

A bit decent JDBC driver won't hit the DB two times on getObject() and getInt().

BalusC