pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
problems returning a resultset from a function
4+ messages / 3 participants
[nested] [flat]

* problems returning a resultset from a function
@ 2004-09-04 16:07 Leo Martin Orfei <orfeileo@yahoo.com>
  2004-09-11 07:40 ` Re: problems returning a resultset from a function Oliver Jowett <oliver@opencloud.com>
  2004-09-13 15:29 ` Re: problems returning a resultset from a function Jim Wilson <jimw@kelcomaine.com>
  0 siblings, 2 replies; 4+ messages in thread

From: Leo Martin Orfei @ 2004-09-04 16:07 UTC (permalink / raw)
  To: pgsql-general@postgresql.org; +Cc: pgsql-jdbc

hi.

I have a problem with postgres functions.
I need return a resultset from a postgres function and
browse the resultset in a java app.
I try with this simple function:

create function test() returns catalog.refcursor as'
declare aux refcursor;
BEGIN
    OPEN aux FOR SELECT name, address FROM table;
    RETURN aux;
END;
'LANGUAGE 'plpgsql';


and the java

CallableStatement cs = null;
ResultSet rs = null;
cs = con.prepareCall("{ ? = call test()}");
cs.registerOutParameter(1, java.sql.Types.OTHER);
rs = cs.executeQuery();
rs.next();
System.out.println("name: " +rs.getString(1));

but throws te following error:

cursor "<unnamed portal 1>" does not exist

I try this too:

Statement s = null;
rs = s.executeQuery("select test()");
rs.next();
System.out.println("name: " +rs.getString(1));

and don't throws an error, but show me the following
result:

name: <unnamed portal 1>

anybody can help me with this?

thnx;




		
__________________________________
Do you Yahoo!?
Yahoo! Mail is new and improved - Check it out!
http://promotions.yahoo.com/new_mail



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

* Re: problems returning a resultset from a function
  2004-09-04 16:07 problems returning a resultset from a function Leo Martin Orfei <orfeileo@yahoo.com>
@ 2004-09-11 07:40 ` Oliver Jowett <oliver@opencloud.com>
  2004-09-11 15:23   ` Re: problems returning a resultset from a function Leo Martin Orfei <orfeileo@yahoo.com>
  1 sibling, 1 reply; 4+ messages in thread

From: Oliver Jowett @ 2004-09-11 07:40 UTC (permalink / raw)
  To: Leo Martin Orfei <orfeileo@yahoo.com>; +Cc: pgsql-jdbc

Leo Martin Orfei wrote:

[.. declare function returning refcursor ..]

> CallableStatement cs = null;
> ResultSet rs = null;
> cs = con.prepareCall("{ ? = call test()}");
> cs.registerOutParameter(1, java.sql.Types.OTHER);
> rs = cs.executeQuery();
> rs.next();
> System.out.println("name: " +rs.getString(1));
> 
> but throws te following error:
> 
> cursor "<unnamed portal 1>" does not exist

Check that you have called Connection.setAutoCommit(false). If 
autocommit is on, your returned cursor will be closed as soon as the 
transaction (auto)commits, so the subsequent select done to fetch the 
portal contents will fail.

A '? = call' escape does not return a resultset. You should use the 
CallableStatement.get...() methods to retrieve the values of the out 
parameter. You will likely see errors complaining about no resultset 
being returned from executeQuery() once you fix the autocommit setting.

The refcursor is returned as a ResultSet (as the out-parameter value) 
i.e. CallableStatement.getObject(1) will return a ResultSet that has the 
contents of the refcursor.

See
http://www.postgresql.org/docs/current/static/jdbc-callproc.html for 
some example code on using callable statements and refcursor-returning 
functions. (note that using PGRefCursorResultSet is deprecated; just use 
getString() to obtain the cursor name).

-O



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

* Re: problems returning a resultset from a function
  2004-09-04 16:07 problems returning a resultset from a function Leo Martin Orfei <orfeileo@yahoo.com>
  2004-09-11 07:40 ` Re: problems returning a resultset from a function Oliver Jowett <oliver@opencloud.com>
@ 2004-09-11 15:23   ` Leo Martin Orfei <orfeileo@yahoo.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Leo Martin Orfei @ 2004-09-11 15:23 UTC (permalink / raw)
  To: Oliver Jowett <oliver@opencloud.com>; +Cc: pgsql-jdbc

Thanks a lot.
This really solve my problem.

regards.

--- Oliver Jowett <oliver@opencloud.com> wrote:

> Leo Martin Orfei wrote:
> 
> [.. declare function returning refcursor ..]
> 
> > CallableStatement cs = null;
> > ResultSet rs = null;
> > cs = con.prepareCall("{ ? = call test()}");
> > cs.registerOutParameter(1, java.sql.Types.OTHER);
> > rs = cs.executeQuery();
> > rs.next();
> > System.out.println("name: " +rs.getString(1));
> > 
> > but throws te following error:
> > 
> > cursor "<unnamed portal 1>" does not exist
> 
> Check that you have called
> Connection.setAutoCommit(false). If 
> autocommit is on, your returned cursor will be
> closed as soon as the 
> transaction (auto)commits, so the subsequent select
> done to fetch the 
> portal contents will fail.
> 
> A '? = call' escape does not return a resultset. You
> should use the 
> CallableStatement.get...() methods to retrieve the
> values of the out 
> parameter. You will likely see errors complaining
> about no resultset 
> being returned from executeQuery() once you fix the
> autocommit setting.
> 
> The refcursor is returned as a ResultSet (as the
> out-parameter value) 
> i.e. CallableStatement.getObject(1) will return a
> ResultSet that has the 
> contents of the refcursor.
> 
> See
>
http://www.postgresql.org/docs/current/static/jdbc-callproc.html
> for 
> some example code on using callable statements and
> refcursor-returning 
> functions. (note that using PGRefCursorResultSet is
> deprecated; just use 
> getString() to obtain the cursor name).
> 
> -O
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



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

* Re: problems returning a resultset from a function
  2004-09-04 16:07 problems returning a resultset from a function Leo Martin Orfei <orfeileo@yahoo.com>
@ 2004-09-13 15:29 ` Jim Wilson <jimw@kelcomaine.com>
  1 sibling, 0 replies; 4+ messages in thread

From: Jim Wilson @ 2004-09-13 15:29 UTC (permalink / raw)
  To: Leo Martin Orfei <orfeileo@yahoo.com>; pgsql-general@postgresql.org

Leo Martin Orfei said:

> hi.
> 
> I have a problem with postgres functions.
> I need return a resultset from a postgres function and
> browse the resultset in a java app.
> I try with this simple function:
> 
> create function test() returns catalog.refcursor as'
> declare aux refcursor;
> BEGIN
>     OPEN aux FOR SELECT name, address FROM table;
>     RETURN aux;
> END;
> 'LANGUAGE 'plpgsql';
> 

My apologies if this has been responded to already as I am not currently
reading pgsql-jdbc and the archive three days behind.  AFAIK there isn't
support for embedded work in jdbc, so it doesn't seem returning a cursor
directly would help (well maybe there is a way...I don't know).

In order to accomplish what I think you want to accomplish (which is to
establish a jdbc resultset object from a stored function) I've done the
following in the past:

Create a pl/pgsql function that returns a rowtype, DECLARED as follows:  

  -- the "table" in the following refers to an existing table definition
  row table%rowtype;
 
In the pl/pgsql script you have something like:

  -- return each row in the result set
  for row in SELECT name, address FROM table loop
    return next row;
  end loop;
  return;

Then in java call this using just a regular statement object:

  // test() is the name of the stored function.
  rs = statement.executeQuery("select * from test()");
  while (rs.next()) {
    ...code to browse/process the rows...
  }

Note the above examples might have a typo or two...but that's the general
idea.  I have not used the stored procedure statement object as you did. 
Perhaps that would be more portable.

Best,

Jim

-- 
Jim Wilson - IT Manager
Kelco Industries
PO Box 160
58 Main Street
Milbridge, ME 04658
207-546-7989 - FAX 207-546-2791
http://www.kelcomaine.com




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


end of thread, other threads:[~2004-09-13 15:29 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-09-04 16:07 problems returning a resultset from a function Leo Martin Orfei <orfeileo@yahoo.com>
2004-09-11 07:40 ` Oliver Jowett <oliver@opencloud.com>
2004-09-11 15:23   ` Leo Martin Orfei <orfeileo@yahoo.com>
2004-09-13 15:29 ` Jim Wilson <jimw@kelcomaine.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