create or replace procedure test_proc_return_cursor(a varchar,mycur OUT sys_refcursor, total_rows OUT int)
as
begin
total_rows :=length(a);
open mycur for 'select substr(x,1,id),id from (select x,generate_series(1,length(x)) as id from (select :a as x))' using a;
end;
import java.sql.*;
public class TestReturnCursor {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
TestReturnCursor test = new TestReturnCursor();
test.runTest();
}
private void runTest() throws SQLException, ClassNotFoundException {
Class.forName( "org.opengauss.Driver");
# 此处替换成准确的ip/port/user/password
Connection conn = DriverManager.getConnection( "jdbc:opengauss://192.168.56.110:26000/postgres?loggerLevel=off", "testproc", "Test@123");
conn.setAutoCommit( false);
CallableStatement cs = (conn.prepareCall ( "{call " + "test_proc_return_cursor(?,?,?)}")) ;
cs.setString(1, "Test return ref_cursor");
cs.registerOutParameter(2,Types.REF_CURSOR);
cs.registerOutParameter(3,Types.INTEGER);
cs.execute();
ResultSet rs=(ResultSet)(cs.getObject(2));
System.out.println( "Total rows:"+cs.getInt(3));
while(rs.next()){
System.out.println(rs.getInt(2)+ " "+rs.getString(1));
}
rs.close();
cs.close();
conn.close();
}
}
javac *.java
java -cp opengaussjdbc.jar:. TestReturnCursor
05
总结
3、最关键的一点,Connection对象需要设置autocommit=false。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29715045/viewspace-3024660/ ,如需转载,请注明出处,否则将追究法律责任。