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