Forum: Building VoltDB Clients

Post: Null value and JDBC

Null value and JDBC
jaewan
Mar 26, 2013
Hi,

I have a problem when checking null return value.

For example, I have a table like this.
+-------+-------+
| col_A | col_B |
+-------+-------+
| 12 | NULL |
+-------+-------+


Invoking
"SELECT col_A, col_B from table"
with JDBC driver, and get the value with the following code.

ResultSet results = query.executeQuery( sql );
while ( results.next() )
{
  if ( results.getObject( "col_B" ) != null )
     value = results.getDouble( "col_B" );
 ...
}


With the table and values before, results.getObject( "col_B" ) did not give me 'null', but some garbage data (-1.7E308), which is not expected according to the JDBC API document.

What's the problem with this?
vtkstef
Mar 26, 2013
Hi,

as per JDBC documentation you need to first get the result, and then call results.wasNull()


ResultSet results = query.executeQuery( sql );
while ( results.next() )
{
  value = results.getDouble( "col_B" );
  if ( ! results.wasNull()) {
     // do something with value
  }
}


Ciao
Stefano
jaewan
Mar 27, 2013
Thanks a lot.

Your code works very well.