resultset.wasNull() giving wrong result

Discussion in 'Java' started by Gen, Mar 6, 2004.

  1. Gen

    Gen Guest

    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
     
    Gen, Mar 6, 2004
    #1
    1. Advertising

  2. Gen

    Ryan Stewart Guest

    "Gen" <> wrote in message
    news:...
    > 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.
     
    Ryan Stewart, Mar 6, 2004
    #2
    1. Advertising

  3. Gen

    Filip Larsen Guest

    "Gen" wrote

    > 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,
    --
    Filip Larsen
     
    Filip Larsen, Mar 7, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ferro
    Replies:
    0
    Views:
    1,316
    Ferro
    Oct 23, 2003
  2. =?ISO-8859-1?Q?Thomas_Gagn=E9?=
    Replies:
    1
    Views:
    492
    Lee Fesperman
    Nov 21, 2003
  3. sixteenmillion

    The giving that keeps on giving

    sixteenmillion, Nov 19, 2007, in forum: C Programming
    Replies:
    0
    Views:
    444
    sixteenmillion
    Nov 19, 2007
  4. Tincy
    Replies:
    0
    Views:
    1,893
    Tincy
    Jan 13, 2009
  5. Michael Tan
    Replies:
    32
    Views:
    1,022
    Ara.T.Howard
    Jul 21, 2005
Loading...

Share This Page