Java accepts the result set returned by the oracle stored procedure (case version)

need

1) Function part

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) Stored procedure part

sys_refcursor can return a table-formatted structure set as a parameter in the stored procedure (I think it is a table type, which is easy to understand, but it is actually a cursor set), cursor can only be used in the implementation of stored procedures, functions, packages, etc. , cannot be used as a parameter. sys_refcursor This thing can be used as a parameter in the package to open the database object-oriented

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) Test in 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) Use jdbc to call the stored procedure to return the result set   

 When calling the stored procedure, use the prepareCall method of CalllabelStatement. Structure: {call stored procedure name (?,?,...)} When setting parameters, use set for input parameters and registerOutParameter for output parameters. To take out the value of the output parameter, you can directly use the get method of CalllabelStatement

@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);
        }    
    }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325824711&siteId=291194637