Thread: JDBC driver. Class Cast Exception when calling CallableStatement.getShort

JDBC driver. Class Cast Exception when calling CallableStatement.getShort

From
"Federico Fissore"
Date:
As subject, i got a stored function that returns an int2
i successfully call it using a callable statement, exactly as it is written in the manual, but when the getShort
expressionis executed, i receive a "Exception in thread Exception in thread "Timer-1" java.lang.ClassCastException:
java.lang.Short
    at org.postgresql.jdbc2.AbstractJdbc2Statement.getShort(AbstractJdbc2Statement.java:1681)
    at org.jcpo.Classes.DBProxy.getIssueStatus(DBProxy.java:131)
    at org.jcpo.Classes.Issue.getIssueStatus(Issue.java:402)
    at org.jcpo.Classes.Issue.canGoOn(Issue.java:431)
    at org.jcpo.Classes.SenderPostOffice.canGoOn(SenderPostOffice.java:109)
    at org.jcpo.Classes.SenderPostOffice.run(SenderPostOffice.java:70)
    at java.util.TimerThread.mainLoop(Unknown Source)
    at java.util.TimerThread.run(Unknown Source)

I thought about a workaround and tried to call it using a Types.INTEGER, instead of Types.SMALLINT, but, CORRECTLY, the
jdbcdriver tells me: 
A CallableStatement function was executed and the return was of type java.sql.Types=5 however type java.sql.Types=4 was
registered.

I'm using pgdev.307.jdbc3 driver with a PostgreSQL Database Server 8.0.0-beta4

I saw in the source code of the driver that, to obtain a short, an Object is casted to Integer and a (short)
Integer.getValue()is called, this way 
(short)((Integer)callResult).intValue ()
I don't know if a Integer.shortValue() would work

The code I've used follows

The Java code
CallableStatement cstm = conn.prepareCall("{? = call myFunction(?)}");
cstm.registerOutParameter(1,Types.SMALLINT);
cstm.setInt(2, anObject.getIntVar());
cstm.execute();
short shortResult = cstm.getShort(1);
cstm.close();

The PG SQL function
CREATE OR REPLACE FUNCTION myFunction(int4) RETURNS int2 AS
$BODY$
    SELECT    shortCol
    FROM    aTable
$BODY$
LANGUAGE 'sql' VOLATILE;

Best regards

Federico

Re: JDBC driver. Class Cast Exception when calling CallableStatement.getShort

From
"Federico Fissore"
Date:
i forgot :)
executing the function manually leads to no errors


> Da: "Federico Fissore" <fissore@hyphen.it>
> Data: Sun, 07 Nov 2004 17:54:47 +0000
> A: pgsql-bugs@postgresql.org
> Oggetto: [BUGS] JDBC driver. Class Cast Exception when calling CallableStatement.getShort
>
> As subject, i got a stored function that returns an int2
> i successfully call it using a callable statement, exactly as it is written in the manual, but when the getShort
expressionis executed, i receive a "Exception in thread Exception in thread "Timer-1" java.lang.ClassCastException:
java.lang.Short
>     at org.postgresql.jdbc2.AbstractJdbc2Statement.getShort(AbstractJdbc2Statement.java:1681)
>     at org.jcpo.Classes.DBProxy.getIssueStatus(DBProxy.java:131)
>     at org.jcpo.Classes.Issue.getIssueStatus(Issue.java:402)
>     at org.jcpo.Classes.Issue.canGoOn(Issue.java:431)
>     at org.jcpo.Classes.SenderPostOffice.canGoOn(SenderPostOffice.java:109)
>     at org.jcpo.Classes.SenderPostOffice.run(SenderPostOffice.java:70)
>     at java.util.TimerThread.mainLoop(Unknown Source)
>     at java.util.TimerThread.run(Unknown Source)
>
> I thought about a workaround and tried to call it using a Types.INTEGER, instead of Types.SMALLINT, but, CORRECTLY,
thejdbc driver tells me: 
> A CallableStatement function was executed and the return was of type java.sql.Types=5 however type java.sql.Types=4
wasregistered. 
>
> I'm using pgdev.307.jdbc3 driver with a PostgreSQL Database Server 8.0.0-beta4
>
> I saw in the source code of the driver that, to obtain a short, an Object is casted to Integer and a (short)
Integer.getValue()is called, this way 
> (short)((Integer)callResult).intValue ()
> I don't know if a Integer.shortValue() would work
>
> The code I've used follows
>
> The Java code
> CallableStatement cstm = conn.prepareCall("{? = call myFunction(?)}");
> cstm.registerOutParameter(1,Types.SMALLINT);
> cstm.setInt(2, anObject.getIntVar());
> cstm.execute();
> short shortResult = cstm.getShort(1);
> cstm.close();
>
> The PG SQL function
> CREATE OR REPLACE FUNCTION myFunction(int4) RETURNS int2 AS
> $BODY$
>     SELECT    shortCol
>     FROM    aTable
> $BODY$
> LANGUAGE 'sql' VOLATILE;
>
> Best regards
>
> Federico
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
>                http://archives.postgresql.org

Re: JDBC driver. Class Cast Exception when calling

From
Kris Jurka
Date:
On Sun, 7 Nov 2004, Federico Fissore wrote:

> As subject, i got a stored function that returns an int2 i successfully
> call it using a callable statement, exactly as it is written in the
> manual, but when the getShort expression is executed, i receive a
> "Exception in thread Exception in thread "Timer-1"
> java.lang.ClassCastException: java.lang.Short
>     at org.postgresql.jdbc2.AbstractJdbc2Statement.getShort(AbstractJdbc2Statement.java:1681)

This has already been fixed in cvs.

Kris Jurka