Java调用存储过程二(返回一行或多行结果集)

来源:互联网 发布:七天教育网络查分 编辑:程序博客网 时间:2024/06/06 03:27
 
为了要得到结果集,需要使用游标进行遍历。因此要使用数据库中的包。现在要根据一个编号得到一行结果集记录。1.建立一个包:create or replace package emp_pkg isType retcursor is ref cursor;procedure pro_read(p_id in emp.empno%type,outcursor out retcursor);end emp_pkg;2.建立一个包体。create or replace package body emp_pkg is  procedure pro_read(p_id in emp.empno%type,outcursor out retcursor)   is    begin       open outcursor for select * from emp where empno=p_id;   end;end emp_pkg;3.Java调用包:public  void getCallableStatement4(){CallableStatement cs=null;Connection conn=this.getConnection();String sql="{call emp_pkg.pro_read(?,?)}";try {cs=conn.prepareCall(sql);cs.setInt(1, 7788);cs.registerOutParameter(2, oracle.jdbc.OracleTypes.CURSOR);cs.executeUpdate();ResultSet rs=(ResultSet) cs.getObject(2);while(rs.next()){System.out.println("编号:"+rs.getInt(1)+"  姓名:"+rs.getString(2));}} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}}

原创粉丝点击