pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jim Wilson <jimw@kelcomaine.com>
To: Leo Martin Orfei <orfeileo@yahoo.com>
To: pgsql-general@postgresql.org
Subject: Re: problems returning a resultset from a function
Date: Mon, 13 Sep 2004 15:29:34 -0000
Message-ID: <twig.1095089374.97040@kelcomaine.com> (raw)
In-Reply-To: <20040904160747.66889.qmail@web51806.mail.yahoo.com>

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




view thread (4+ messages)

Message-ID: <twig.1095089374.97040@kelcomaine.com>
Permalink:  ../twig.1095089374.97040@kelcomaine.com/
Also on:    postgresql.org/message-id/twig.1095089374.97040@kelcomaine.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: jimw@kelcomaine.com, orfeileo@yahoo.com, pgsql-general@postgresql.org
  Subject: Re: problems returning a resultset from a function
  In-Reply-To: <twig.1095089374.97040@kelcomaine.com>
  References: <20040904160747.66889.qmail@web51806.mail.yahoo.com> <twig.1095089374.97040@kelcomaine.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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