The Artima Developer Community
Sponsored Link

Java Buzz Forum
Using postgresql ref cursors in java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Andrej Koelewijn

Posts: 594
Nickname: andrejk
Registered: Nov, 2002

Andrej Koelewijn is a Java and Oracle consultant
Using postgresql ref cursors in java Posted: Apr 6, 2004 2:08 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Andrej Koelewijn.
Original Post: Using postgresql ref cursors in java
Feed Title: Andrej Koelewijn
Feed URL: http://feeds.feedburner.com/AndrejKoelewijn
Feed Description: On Oracle, Java and OpenSource
Latest Java Buzz Posts
Latest Java Buzz Posts by Andrej Koelewijn
Latest Posts From Andrej Koelewijn

Advertisement

Here's an example how to use postgresql ref cursors in jdbc.

I've created the following plpgsql function (example taken from postgresql documentation :

CREATE FUNCTION reffunc() RETURNS refcursor AS '
DECLARE
  ref refcursor;
BEGIN
  OPEN ref FOR SELECT * FROM table1;
  RETURN ref;
END;
' LANGUAGE plpgsql;

You can use this function as follows in java (documentation) :

import java.sql.*;
public class RefFunc {
   public static void main(String arg[])
       throws Exception {
      System.out.println("RefFunc");
      Class
          .forName("org.postgresql.Driver");
      Connection db = DriverManager
          .getConnection(
              "jdbc:postgresql:dev1",
              "scott", "tiger");
      //
      // Query rows using prepared statement
      //
      System.out.println("Using query:");
      PreparedStatement stmt = db
          .prepareStatement("select * from table1");
      ResultSet rset = stmt
          .executeQuery();
      while (rset.next()) {
         System.out.println(rset
             .getString(1));
       }
      rset.close();
      stmt.close();
      //
      // Query rows using cursor
      //
      System.out.println("Using cursor:");
      db.setAutoCommit(false);
      CallableStatement proc = db
          .prepareCall("{ ? = call reffunc() }");
      proc.registerOutParameter(1,
          Types.OTHER);
      proc.execute();
      ResultSet rset2 = (ResultSet) proc
          .getObject(1);
      while (rset2.next()) {
         System.out.println(rset2
             .getString(1));
       }
      rset2.close();
      proc.close();
      db.close();
    }
}

Read: Using postgresql ref cursors in java

Topic: The Burger: Search-and-Replace Development and Strategy Previous Topic   Next Topic Topic: What is a Robot?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use