How to call stored procedures in Oracle in pl/SQL and java

Source: Internet
Author: User

Case: Add students and return to all students in the class.

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 -- output variable, system reference cursor
)
As
Begin
Insert into stu (sid, sname, cid)
Values (p_sid, p_sname, p_cid );
Commit;

-- Place the address of the query result set in the referenced cursor variable and pass it out.
Open p_data for select * from stu where cid = p_cid;

End;


-- PL/SQL call
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;

 

-- Called in java
CallableStatement cstmt = null;
String spName = "{call add_stu (?,?,?,?)} ";
Cstmt = conn. prepareCall (spName );
Cstmt. setInt (1, employee ID );
............
Cstmt. registerOutParameter (4, Oracle. jdbc. OracleTypes. CURSOR); -- set the value of 4th question marks
Cstmt.exe cuteUpdate ();
ResultSet rs = (ResultSet) cstmt. getObject (4 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.