resultset.wasNull() giving wrong result

G

Gen

Could anyone tell me if there is any mistake in the following
function?
wasNull returns true even for non-null values so the function is
always returning null.
I have spent half a day but am unable to figure out why.

private Float getFloat(ResultSet rs, String col) throws SQLException
{
Float retVal = new Float(rs.getFloat(col));
System.out.println("Val1=" + retVal);
if (rs.wasNull()) retVal=null;
System.out.println("Val2=" + retVal);
return retVal;
}
----- Sample Output ------
Val1=20.0
Val2=null
(Should be Val2=20.0)
------------------

I would appreciate any comments.
Thanks

Gen
 
R

Ryan Stewart

Gen said:
Could anyone tell me if there is any mistake in the following
function?
wasNull returns true even for non-null values so the function is
always returning null.
I have spent half a day but am unable to figure out why.

private Float getFloat(ResultSet rs, String col) throws SQLException
{
Float retVal = new Float(rs.getFloat(col));
System.out.println("Val1=" + retVal);
if (rs.wasNull()) retVal=null;
System.out.println("Val2=" + retVal);
return retVal;
}
----- Sample Output ------
Val1=20.0
Val2=null
(Should be Val2=20.0)
------------------

I would appreciate any comments.
Thanks

Gen

I've never used that method, but that seems like it should work. You might
try checking your ResultSet source code if you can get ahold of it, or at
least try to find some documentation for it. Maybe the drivers you're using
don't provide a real implementation of that method.
 
F

Filip Larsen

private Float getFloat(ResultSet rs, String col) throws SQLException
{
Float retVal = new Float(rs.getFloat(col));
System.out.println("Val1=" + retVal);
if (rs.wasNull()) retVal=null;
System.out.println("Val2=" + retVal);
return retVal;
}
----- Sample Output ------
Val1=20.0
Val2=null
(Should be Val2=20.0)
------------------

Perhaps you can try see if rs.getObject(col) returns null at the right
times? Something like

private Float getFloat(ResultSet rs, String col) throws SQLException
{
Object value = rs.getObject(col);
if (value instanceof Float) return (Float)value;
if (value instanceof Number) new Float(((Number)value).floatValue());
return null;
}


Regards,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top