Oracle pl/sql编程 25--调用返回结果集的存储过程

来源:转载

         对于查询多条数据我们需要将他放到一个游标中,这样我们就需要使用游标和存储过程,这就需要定义一个包

先定义包头

CREATE OR REPLACE PACKAGE mypack IS TYPE mycursor IS REF CURSOR; PROCEDURE myproc(outcursor IN OUT mycursor); END mypack;
在定义包体

CREATE OR REPLACE PACKAGE BODY mypack IS PROCEDURE myproc( outcursor IN OUT mycursor ) IS BEGIN OPEN outcursor FOR SELECT*FROM emp WHERE deptno=10; RETURN; END myproc; END; 

java代码,这里只要注意注册输出参数的的时候是数据类型是ORACLETYPE里面的cursor,取出来的时候取得是object 然后将他转成resutset


public static void main(String args[]){ Connection conn; CallableStatement call; //加载驱动 try { Class.forName("oracle.jdbc.driver.OracleDriver"); //获得连接 conn=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "m123"); //各种操作 call=conn.prepareCall("{call mypack.myproc(?)}"); call.registerOutParameter(1, OracleTypes.CURSOR);; call.execute(); ResultSet rs=(ResultSet)call.getObject(1); while(rs.next()){ System.out.println(rs.getString("ename")); } //各种关 } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } }



分享给朋友:
您可能感兴趣的文章:
随机阅读: