Skip to content

ResultSet.getObject returns wrong type when column is DOUBLE or BIGINT. #81

@erikjber

Description

@erikjber

ResultSet.getObject(int columnindex) should return an object that encapsulates the database column.
This does not hold for DOUBLE and BIGINT columns. Instead of returning java.lang.Double and java.lang.Long, getObject returns java.lang.Float and java.lang.Integer, respectively.

The database holds the correct values, but they get truncated to fit the returned data types.

Here's example code to provoke the bug:

    Class.forName(driver).newInstance();
    Connection con = DriverManager.getConnection(database,login, password);
    con.setAutoCommit(false);
    PreparedStatement stmt = con.prepareStatement("CREATE TABLE FOO (A DOUBLE, B BIGINT)");
    try
    {
        stmt.execute();
    }
    catch(SQLException e)
    {
        //don't care, this just means the table already exists
    }
    stmt.close();
    stmt = con.prepareStatement("DELETE FROM FOO");
    stmt.execute();
    stmt.close();
    con.commit();
    //add long value that is outside the bounds of int
    long bigLong = Integer.MAX_VALUE*2l;
    //add double value that is outside the bounds of float
    double bigDouble = Float.MAX_VALUE*2.0;
    stmt = con.prepareStatement("INSERT INTO FOO(A,B) VALUES (?,?)");
    stmt.setDouble(1,bigDouble);
    stmt.setLong(2,bigLong);
    stmt.execute();
    stmt.close();
    con.commit();
    //check that the correct values are read back
    stmt = con.prepareStatement("SELECT A,B FROM FOO");
    ResultSet res = stmt.executeQuery();
    res.next();
    Object a = res.getObject(1);
    Object b = res.getObject(2);
    System.out.println(a.getClass());
    System.out.println(b.getClass());
    System.out.println(a);
    System.out.println(b);

The code will output:

  class java.lang.Float
  class java.lang.Integer
  Infinity
  -2

The same code works fine on SQLite on Linux, breaks on Android.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions