在数据库中创建存储过程:
---创建包和游标
CREATE OR REPLACE PACKAGE TESTPACKAGE AS
TYPE Test_CURSOR IS REF CURSOR;
end TESTPACKAGE;
----创建存储过程 输出是游标----
create or replace procedure payCursor(info_cursor out TESTPACKAGE.Test_CURSOR)
is begin
open info_cursor for SELECT * FROM pay_bank_base_info;
end payCursor;
使用java调用示例:
public Map Test(Map map) throws ClassNotFoundException {
System.out.println("Test:storeValueTrans-会员卡查询");
Map resMap = new HashMap();
try {
Connection conn = DBUtil.getConn("16.83.13.54", "orcl", "pmpdata", "pmpdata123");//创建数据库连接
//调用存储过程,存储过程中只有一个输出参数,所以在调用的时候传进一个参数
CallableStatement call = conn.prepareCall(" {call payCursor(?)} ");
//输出参数的话要注册,注意传参数的时候是从1开始的,不是从0,参数的游标的类型只能是oracle.jdbc.OracleTypes.CURSOR
call.registerOutParameter(1,oracle.jdbc.OracleTypes.CURSOR);
//注册后要更新
// call.execute();
// 获取执行的存储过程的返回值,调用的时候要将call强转成OracleCallableStatement,获取游标的值,不能从第0个获取
ResultSet rSet = ((OracleCallableStatement)call).getCursor(1);
int i =1;while (rSet.next()) {resMap.put("count"+i, rSet.getString(i));i++;}} catch (SQLException e) {e.printStackTrace();}return resMap;}
测试程序:
import java.util.HashMap;
import java.util.Map;
import org.apache.catalina.core.ApplicationContext;
public class Ttest {
public static void main(String[] args) throws ClassNotFoundException {
// TODO Auto-generated method stub
Map map = new HashMap<>();
ZhongBeiService zbs = new ZhongBeiService();
Map smMap =zbs.Test(map);
for (Object st : smMap.keySet()) {
System.out.println(st);
}
}
}
测试结果: