一、函数
第一种方式:获得connection
Connection conn = session.connection();
CallableStatement call = conn.prepareCall("{?=call getemp.getempc(?)}");
call.registerOutParameter(1, OracleTypes.CURSOR);// 设置输出变量类型
call.setInt(2, 10);
call.executeQuery();
ResultSet rs = (ResultSet) call.getObject(1);
while (rs.next()) {
System.out.println(rs.getString(3));
}
对应函数包体:
create or replace package body getemp is
function getempc(dno number) return sys_refcursor
is
type emp_cursor_type is ref cursor;
emp_cursor emp_cursor_type;
begin
open emp_cursor for select * from scott.emp where deptno=dno;
return emp_cursor;
end;
end getemp;
二、存储过程
对应的存储过程:
create or replace procedure getdeptbyid(d out sys_refcursor,dno number,num number )
is
begin
open d for select * from dept where deptno=dno and 1=num;
end;
第一种方式:获得connection
Connection conn = session.connection();
CallableStatement call = conn.prepareCall("{call getdeptbyid(?,?,?)}");
call.registerOutParameter(1, OracleTypes.CURSOR);// 设置输出变量类型
call.setInt(2, 10);
call.setInt(3, 1);
call.executeQuery();
ResultSet rs = (ResultSet) call.getObject(1);
while (rs.next()) {
System.out.println(rs.getString(2));
}