Oracle中的預存程序在pl/sql和java中如何調用

來源:互聯網
上載者:User

案例:新增學生,然後返回該班級的所有學生。

create or replace procedure add_stu(
p_sid stu.sid%type,        
p_sname stu.sname%type,
p_cid stu.cid%type,
p_data out sys_refcursor     -- 輸出變數,系統引用遊標
)
as
begin
   insert into stu(sid,sname,cid)
   values(p_sid,p_sname,p_cid);
   commit;
 
   --將查詢的結果集的地址放到引用遊標變數中,再傳遞出去
   open p_data for select * from stu where cid=p_cid;
 
end;


--PL/SQL 調用
declare
  stu_data sys_refcursor;
  stu_row stu%rowtype;
begin
  add_stu(52,'b',1,stu_data);
  fetch stu_data into stu_row;
  while(stu_data%found)
  loop
     dbms_output.put_line(stu_row.sname);
     fetch stu_data into stu_row;
  end loop;
  close stu_data;
end;

 

--java中調用
CallableStatement cstmt = null;
String spName = “{call add_stu(?,?,?,?)}";
cstmt = conn.prepareCall(spName);
cstmt.setInt(1, 工號);
 …………
cstmt.registerOutParameter(4, Oracle.jdbc.OracleTypes.CURSOR);   --設定第4個問號的值
cstmt.executeUpdate();
ResultSet rs = (ResultSet)cstmt.getObject(4);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.