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