java接受oracle存储过程返回的结果集(案例版)

需求

141450_FTWM_585635.png

1)函数部分

create or replace function f_dept_money_min
return number
is
 v_min number;
begin
  select min(money) into v_min from biao;
  return v_min;
end;

2)存储过程部分

sys_refcursor,可以在存储过程中作为参数返回一个table格式的结构集(我把他认为是table类型,容易理解,其实是一个游标集), cursor 只能用在存储过程,函数,包等的实现体中,不能做参数使用。sys_refcursor 这东西可以使用在包中做参数,进行数据库面向对象开放

create or replace procedure pro_money(out_return out sys_refcursor)

is
begin
  --将最低工资提高200
   for tab in (
  select id from biao where money=f_dept_money_min())
  loop
    update biao set money=money+200 where id=tab.id;
    end loop;
  --将最低工资提高100
  for tab1 in(
    select id from biao where money>f_dept_money_min()+200
    and money<f_dept_money_min()+400
    )
    loop
    update biao set money=money+200 where id=tab1.id;
    end loop;
    commit;
   --返回列表编号,姓名,工资
   open out_return for 'select id,name,money from biao';
end;

3)plsql中测试

declare
  cur1   SYS_REFCURSOR;
  i      biao%rowtype;
begin
 sql_test(cur1);
  loop
    fetch cur1
      into i;
    exit when cur1%notfound;
    dbms_output.put_line('----------------:' || i.id||i.name||i.money);--id为表baio中的id列
  end loop;
  close cur1;
end;

4)用jdbc来调用存储过程返回结果集   

 调用存储过程时,要用CallabelStatement的prepareCall 方法。结构:{call 存储过程名(?,?,...)}在设置参数的时候,输入参数用set,输出参数要registerOutParameter。取出输出参数的值可以直接用CallabelStatement的get方法

@Test
    public void proMoney(){
        java.sql.Connection conn=null;
        java.sql.CallableStatement stmt=null;
        ResultSet rs=null;
        try {
            conn=DaoUtil.getConnection();
            stmt=conn.prepareCall("{call pro_money(?)}");
            stmt.registerOutParameter(1, OracleTypes.CURSOR);
            stmt.execute();            
            rs = (ResultSet) stmt.getObject(1);
            while(rs.next()){
            String name = rs.getString("name");
            double ids = rs.getDouble("id");
            int money = rs.getInt("money");
            System.out.println("name="+name+"ids="+ids+"money="+money);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DaoUtil.closeAll(rs, stmt, conn);
        }    
    }

 

转载于:https://my.oschina.net/u/585635/blog/910811

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值