Bug #1913 Wrong object classes returned from ResultSet.getObject methods.
Submitted: 21 Nov 2003 15:19 Modified: 21 Nov 2003 17:51
Reporter: Kevin Grittner Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S2 (Serious)
Version:3.0.9-stable OS:
Assigned to: CPU Architecture:Any

[21 Nov 2003 15:19] Kevin Grittner
Description:
The JDBC specs define which Java object classes should be returned from ResultSet.getObject for which SQL types.  See table B-3 in the JDBC 3.0 specification, which is consistent with previous versions.

Incorrect classes are returned for columns with a SQL type of TINYINT or SMALLINT.  They should return an Integer object.  TINYINT returns Byte or Short, depending on whether the column is signed.  SMALLINT returns Short or Integer, depending on whether the column is signed.

How to repeat:
From within Java:

try {statement.executeUpdate("DROP TABLE BadObjectTypes");} catch (SQLException e){}
statement.executeUpdate("CREATE TABLE BadObjectTypes (f1 TINYINT NOT NULL, f2 SMALLINT NOT NULL)");
statement.executeUpdate("INSERT INTO BadObjectTypes VALUES (0,0)");
ResultSet rs = statement.executeQuery("SELECT * FROM BadObjectTypes");
rs.next();
System.out.println(rs.getObject(1).getClass());
System.out.println(rs.getObject(2).getClass());
rs.close();

I would expect:

class java.lang.Integer
class java.lang.Integer

I get:

class java.lang.Byte
class java.lang.Short

Suggested fix:
The switch statement in ResultSet.getObject(int) should be modified for the appropriate cases:

        case Types.TINYINT:

            return new Integer(getInt(columnIndex));

        case Types.SMALLINT:

            return new Integer(getInt(columnIndex));

While this will allow code written to the JDBC specification to work, it has the potential to break code written to existing versions of MySQL Connector/J.  It may be worth adding a connection property to perserve the old behavior.
[21 Nov 2003 17:51] Mark Matthews
Thank you for your bug report. This issue has been committed to our
source repository of that product and will be incorporated into the
next release.

If necessary, you can access the source repository and build the latest
available version, including the bugfix, yourself. More information 
about accessing the source trees is available at
    http://www.mysql.com/doc/en/Installing_source_tree.html