Oracle存储过程返回结果集

存储过程testcur,接受一个输入参数,为out类型,返回sys_refcursor类型的光标。简章示意如下:
create or replace procedure testcur(ref_cursor out sys_refcursor)
as 
begin
	open ref_cursor for select sysdate from dual;
end;


java代码如下:
 
   try {
          DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
          Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl", "scott", "tiger");
          
          CallableStatement stmt = conn.prepareCall("BEGIN testcur(?); END;");
          //也可以这样调用
          //CallableStatement stmt = conn.prepareCall("{ call testcur(?) }");
          stmt.registerOutParameter(1, -10 /* OracleTypes.CURSOR = -10 */); //REF CURSOR(OracleTypes.CURSOR==-10)
          stmt.execute();
          ResultSet rs = (ResultSet) stmt.getObject(1);
          while (rs.next()) {
            System.out.println(rs.getObject(1); 
          }
          rs.close();
          rs = null;
          stmt.close();
          stmt = null;
          conn.close();
          conn = null;
        }
        catch (SQLException e) {
          System.out.println(e.getLocalizedMessage());
        }

你可能感兴趣的:(oracle存储过程)