为了要得到结果集,需要使用游标进行遍历。因此要使用数据库中的包。

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

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();

}

}

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐