Re: Getting a ResultSet for a refcursor element.

From: Nic Ferrier <nferrier(at)tapsellferrier(dot)co(dot)uk>
To: Dave Cramer <Dave(at)micro-automation(dot)net>
Cc: "pgsql-jdbc(at)postgresql(dot)org" <pgsql-jdbc(at)postgresql(dot)org>
Subject: Re: Getting a ResultSet for a refcursor element.
Date: 2002-10-09 01:44:44
Message-ID: 87u1jw2xo3.fsf@pooh-sticks-bridge.tapsellferrier.co.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

Here's my context diff for getting ResultSet's whole from another
ResultSet (via a proc returning a refcursor).

Here's some example code:

import java.sql.*;

public class proctest
{
public static void main (String[] argv) throws Exception
{
Class driver = Class.forName("org.postgresql.Driver");
Connection con
= DriverManager.getConnection("jdbc:postgresql:test",
"someone",
"something");
Statement st = con.createStatement();
con.setAutoCommit(false);
// f() is a function that returns a refcursor.
ResultSet rs = st.executeQuery("select f();");
if (! rs.next())
throw new SQLException("whoops! there were no rows.");
try
{
Object v = rs.getObject(1);
if (v instanceof ResultSet) {
ResultSet rs2 = (ResultSet) v;
while (rs2.next()) {
System.out.println(rs2.getString(1));
}
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
con.commit();
st.close();
con.close();
}
}

Do I need to do a documentation patch? Does anybody else have a good
idea for how this should be described in the doc?

Nic

Here's the diff:

Index: src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java
===================================================================
RCS file: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java,v
retrieving revision 1.8
diff -c -r1.8 AbstractJdbc2ResultSet.java
*** src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java 2002/09/11 05:38:45 1.8
--- src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java 2002/10/09 01:21:13
***************
*** 142,147 ****
--- 142,158 ----
{
return getString(columnIndex);
}
+ else if (type.equals("refcursor"))
+ {
+ // We must return a ResultSet with the results packaged.
+ // We should probably check that auto commit is turned off.
+ String cursorName = getString(columnIndex);
+ Statement st
+ = new Jdbc2Statement((Jdbc2Connection)this.connection);
+ return st.executeQuery("FETCH ALL IN \""
+ + cursorName
+ + "\";");
+ }
else
{
return connection.getObject(field.getPGType(), getString(columnIndex));

In response to

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message fabio viquez 2002-10-09 01:50:26 An error occured while getting the authentification request
Previous Message Julian Brown 2002-10-08 23:38:06 Re: I am totally lost