pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
CallableStatement: java.sql.Types=12 however type java.sql.Types=1111 was registered
4+ messages / 2 participants
[nested] [flat]

* CallableStatement: java.sql.Types=12 however type java.sql.Types=1111 was registered
@ 2004-12-21 21:19  Jerome Colombie <jcolombie@gmx.ch>
  0 siblings, 1 reply; 4+ messages in thread

From: Jerome Colombie @ 2004-12-21 21:19 UTC (permalink / raw)
  To: pgsql-jdbc

Hi

I get the following error:

org.postgresql.util.PSQLException: A CallableStatement function was 
executed and the return was of type java.sql.Types=12 however type 
java.sql.Types=1111 was registered.

when running the following code:

               conn.setAutoCommit(false);
                CallableStatement stmt = conn.prepareCall("{ ? = call 
myfunction1() }");
                stmt.registerOutParameter(1, Types.OTHER);
                stmt.execute();
                ResultSet rs = (ResultSet) stmt.getObject(1);
                while (rs.next()) {
                    result = result + rs.getString(1);
                    result = result + rs.getDouble(2);
                }

create type b_line as (account_text varchar(255), amount numeric);

CREATE OR REPLACE FUNCTION myfunction1()
  RETURNS setof b_line AS
$BODY$DECLARE
  tmp1 numeric;
  account RECORD;
  r b_line%rowtype;
BEGIN
  tmp1 = 0.00;
  FOR i IN 30..39 LOOP
    FOR account IN SELECT id, account_id, name, type, amount_cred FROM 
bo.obj_ledger WHERE account_id like (i || '__') ORDER BY id LOOP
      IF account.type = 'P' THEN
        tmp1 = tmp1 + account.amount_cred;
      ELSE
        tmp1 = tmp1 - account.amount_cred;
      END IF;
    END LOOP;
  END LOOP;
  r.account_text = 'Line1:';
  r.amount = tmp1;
  return next r;
  RETURN;
END;$BODY$
  LANGUAGE 'plpgsql' VOLATILE;



I'm using the following versions:
pg80b1.308.jdbc3.jar
PostgreSQL 8.0.0rc1 on i686-pc-mingw32, compiled by GCC gcc.exe (GCC) 
3.3.1 (mingw special 20030804-1)

Thanks for your help.

Regards,
 Jerome




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: CallableStatement:  java.sql.Types=12 however type
@ 2004-12-21 21:35  Kris Jurka <books@ejurka.com>
  parent: Jerome Colombie <jcolombie@gmx.ch>
  0 siblings, 1 reply; 4+ messages in thread

From: Kris Jurka @ 2004-12-21 21:35 UTC (permalink / raw)
  To: Jerome Colombie <jcolombie@gmx.ch>; +Cc: pgsql-jdbc



On Tue, 21 Dec 2004, Jerome Colombie wrote:

> org.postgresql.util.PSQLException: A CallableStatement function was 
> executed and the return was of type java.sql.Types=12 however type 
> java.sql.Types=1111 was registered.
> 
> when running the following code:
> 
>                conn.setAutoCommit(false);
>                 CallableStatement stmt = conn.prepareCall("{ ? = call 
> myfunction1() }");
>                 stmt.registerOutParameter(1, Types.OTHER);
>                 stmt.execute();
>                 ResultSet rs = (ResultSet) stmt.getObject(1);
>                 while (rs.next()) {
>                     result = result + rs.getString(1);
>                     result = result + rs.getDouble(2);
>                 }
> 
> create type b_line as (account_text varchar(255), amount numeric);
> 
> CREATE OR REPLACE FUNCTION myfunction1()
>   RETURNS setof b_line AS

The JDBC driver is expecting a single scalar value returned from your 
function.  It retrieves the first column in the first row and detects that 
it is a varchar, not the Types.OTHER that you had registered.  You really 
don't want to use the CallableStatement interface for this operation.  Try 
instead:

Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM myfunction()");

Kris Jurka




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: CallableStatement: java.sql.Types=12 however type java.sql.Types=1111
@ 2004-12-22 18:39  Jerome Colombie <jcolombie@gmx.ch>
  parent: Kris Jurka <books@ejurka.com>
  0 siblings, 1 reply; 4+ messages in thread

From: Jerome Colombie @ 2004-12-22 18:39 UTC (permalink / raw)
  To: Kris Jurka <books@ejurka.com>; +Cc: pgsql-jdbc

Hi Kris,

Thanks you very much for your help. It works perfectly now. I just 
wonder why it didn't work with the callable statement. In my opinion it 
should also work with the procedure call, since it is a stored 
procedure. Of course it is not needed, since the statement

"SELECT * FROM myfunction()"

works perfectly, but according to an O'Reilly Article 
(http://www.onjava.com/pub/a/onjava/2003/08/13/stored_procedures.html?page=last) 
the callable statement should also work, but maybe this is obsolete now.

Thanks again!
 Jerome



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: CallableStatement:  java.sql.Types=12 however type
@ 2004-12-22 20:16  Kris Jurka <books@ejurka.com>
  parent: Jerome Colombie <jcolombie@gmx.ch>
  0 siblings, 0 replies; 4+ messages in thread

From: Kris Jurka @ 2004-12-22 20:16 UTC (permalink / raw)
  To: Jerome Colombie <jcolombie@gmx.ch>; +Cc: pgsql-jdbc



On Wed, 22 Dec 2004, Jerome Colombie wrote:

> Thanks you very much for your help. It works perfectly now. I just 
> wonder why it didn't work with the callable statement. In my opinion it 
> should also work with the procedure call, since it is a stored 
> procedure.

Postgresql doesn't really support stored procedures, only functions.  In 
time this function support was hacked to return sets, but it is not really 
true stored procedure support.  Notably from the caller's perspective how 
can you tell what the difference is between a function that returns an int 
and a function that returns a setof int, but returns only one row.  In 
both cases you get a one row, one column result.  In the first case 
CallableStatement.getObject should return Integer, but in the second case 
you're suggesting it should return ResultSet.  I'm unclear on how to make 
this determination in the client.

> works perfectly, but according to an O'Reilly Article 
> (http://www.onjava.com/pub/a/onjava/2003/08/13/stored_procedures.html?page=last) 
> the callable statement should also work, but maybe this is obsolete now.
> 

This examples shows returning a refcursor, not a setof <type>.  This does
work, and is different because a refcursor is a single scalar value (a
cursor name) that may then be transformed into a true ResultSet.

Kris Jurka



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2004-12-22 20:16 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-12-21 21:19 CallableStatement: java.sql.Types=12 however type java.sql.Types=1111 was registered Jerome Colombie <jcolombie@gmx.ch>
2004-12-21 21:35 ` Re: CallableStatement:  java.sql.Types=12 however type Kris Jurka <books@ejurka.com>
2004-12-22 18:39   ` Re: CallableStatement: java.sql.Types=12 however type java.sql.Types=1111 Jerome Colombie <jcolombie@gmx.ch>
2004-12-22 20:16     ` Re: CallableStatement:  java.sql.Types=12 however type Kris Jurka <books@ejurka.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox