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

论坛 期权论坛 脚本     
匿名技术用户   2020-12-28 10:45   101   0
为了要得到结果集,需要使用游标进行遍历。因此要使用数据库中的包。

现在要根据一个编号得到一行结果集记录。

1.建立一个包:

create or replace package emp_pkg is
Type 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 block
   e.printStackTrace();
  }
 }


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP