Calling stored procedures in Java

Source: Internet
Author: User
First, you must connect to the database. Private Static connection conn; static {// Step 1: load the driver try {class. forname ("oracle. JDBC. driver. oracledriver "); // obtain the connection object conn = drivermanager. getconnection ("JDBC: oracle: thin :@ localhost: 1521: orcl", "Scott", "Scott");} catch (classnotfoundexception E) {// todo auto-generated catch blocke. printstacktrace ();} catch (sqlexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} instance 1: [Create or rep Export procedure selectemp (emp_no in number, ename out varchar2, job out varchar2, Sal out number, deptno out number) isbeginselect ename, job, Sal, deptno into ename, job, Sal, deptno from EMP where empno = emp_no; end selectemp;] // call the Stored Procedure public static void procedure () throws sqlexception {// create the callablestatement parameter in out by passing the callablestatement CAS = conn through a placeholder. preparecall ("{call selectemp (?,?,?,?,?)} "); // Int Index = 1 from 1; // assign CAs to the placeholder. setint (index ++, 7369); // the value assigned is the input parameter // The output parameter CAS. registeroutparameter (index ++, Oracle. JDBC. oracletypes. varchar); CAS. registeroutparameter (index ++, Oracle. JDBC. oracletypes. varchar); CAS. registeroutparameter (index ++, Oracle. JDBC. oracletypes. number); CAS. registeroutparameter (index ++, Oracle. JDBC. oracletypes. number); // execute the operation based on the passed parameter value. If the first result is a resultset object, true is returned. If the first result is an update count or no Returns falseboolean flag1_cas.exe cute (); system. Out. println (FLAG); If (! Flag) {// string ename = CAS. getstring (2); string job = CAS. getstring (3); int sal = CAS. getint (4); int deptno = CAS. getint (5); system. out. println (ename); system. out. println (job); system. out. println (SAL); system. out. println (deptno);} instance 2: [declare a stored procedure create or replace package emppackage istype empcursor is ref cursor; Procedure selectemps (emplist out empcursor); End emppackage; package body: Create or replace package body EMP Package isprocedure selectemps (emplist out empcursor) isbeginopen emplist for select * from EMP; end emppackage;] public static void testprocedure () throws sqlexception {// create the callablestatement parameter in out by passing the callablestatement CAS = conn through a placeholder. preparecall ("{call emppackage. selectemps (?)} "); // Int Index = 1 from 1; // assign CAs to the placeholder. registeroutparameter (index ++, Oracle. JDBC. oracletypes. cursor); // if the operation is returned Based on the passed parameter value, false indicates that there is nothing in it. If the return value is true, the returned resultset result set Boolean flag1_cas.exe cute (); system. out. println (FLAG); // oraclecallablestatement is the object of the stored procedure in Oracle. // The getcursor method in the call returns the resultset result set resultset rs = (oraclecallablestatement) CAS ). getcursor (1); While (RS. next () {system. out. println (RS. getint (1 ));}}

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.