1. 从www.postgresql.org下载最新的postgresql-snapshot.tar.gz, ant from apache;
2. setting ANT_HOME
3. ../configure --with-java; make; make install
FUNCTION 和 JAVA 代码参考了jdbc.postgresql.org mailing list里面的内容.
CREATE TABLE users (id int, name varchar(32));
CREATE OR REPLACE FUNCTION sp_users_select () RETURNS refcursor
AS '
declare ref refcursor;
begin
open ref for select * from users;
return ref;
end;
' LANGUAGE plpgsql;
import java.lang.*;
import java.sql.*;
public class pc
{
public static void main(String[] args)
throws Exception
{
String driver = "org.postgresql.Driver";
String url = "jdbc:postgresql://localhost:5432/nop";
String user = "nop";
String passwd = "nop";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, passwd);
conn.setAutoCommit(false); // return refcursor must within a transaction
CallableStatement proc = conn.prepareCall("{ ? = call sp_users_select () }");
proc.registerOutParameter(1, Types.OTHER);
proc.execute();
ResultSet result = (ResultSet) proc.getObject(1);
System.out.println(result);
while(result.next())
{
System.err.println("Name : " + result.getString(2));
}
conn.commit();
}
}