Thread: ClassCastException in getShort(int)

ClassCastException in getShort(int)

From
Christian Niles
Date:
Hi All,

I've found a bug in AbstractJdbc1Statement.getShort(int) which causes a
ClassCastException. I noticed that this file has been deleted in the
public CVS, and have confirmed that the bug exists in
AbstractJdbc2Statement.

The bug seems to be that getShort(int) casts the Short to an Integer:

    public short getShort(int parameterIndex) throws SQLException
    {
        checkClosed();
        checkIndex (parameterIndex, Types.SMALLINT, "Short");
        if (callResult == null)
            return 0;
        return (short)((Integer)callResult).intValue ();
    }

[ from
http://gborg.postgresql.org/project/pgjdbc/cvs/co.php/pgjdbc/org/
postgresql/jdbc2/AbstractJdbc2Statement.java?sa=1&r=1.36 ]

All the other methods seem to cast the appropriate type, except
getByte(int), which also casts to Integer (I haven't tested
getByte(int) though).

I checked the return value using getObject(int) and the result object
is indeed a java.lang.Short.

FYI, I'm using the 215 build for 7.4 (pg74.215.jdbc3.jar). I don't
suspect my code has anything to do with the problem, but here it is:

     private short db_getLibraryID(String library) throws SQLException {

         CallableStatement stmt = connection.prepareCall("{?= call
get_library_id(?) }");
         stmt.registerOutParameter(1, Types.SMALLINT);
         stmt.setString(2, library);
         stmt.execute();
         return stmt.getShort(1);

     }

best,
christian.


Re: ClassCastException in getShort(int)

From
Kris Jurka
Date:

On Thu, 21 Oct 2004, Christian Niles wrote:

> I've found a bug in AbstractJdbc1Statement.getShort(int) which causes a
> ClassCastException.
>
> The bug seems to be that getShort(int) casts the Short to an Integer:

I've fixed this in both 7.4 and 8.0 trees.  Thanks.

> All the other methods seem to cast the appropriate type, except
> getByte(int), which also casts to Integer (I haven't tested
> getByte(int) though).

The getByte case is a can't happen situation, and I've put in an Exception
if the code actually gets there.

Kris Jurka