Java call Oracle Stored Procedure tutorial

Source: Internet
Author: User
Among Java and Oracle, the most common method is to call the Oracle stored procedure. The following briefly describes how Java calls the Oracle stored procedure.

Among Java and Oracle, the most common method is to call the Oracle stored procedure. The following briefly describes how Java calls the Oracle stored procedure.

Among Java and Oracle, the most common method is to call the Oracle stored procedure. The following briefly describes how Java calls the Oracle stored procedure.

I. Java calls the Oracle stored procedure [without output parameters]

The process name is pro1, the number of parameters is 1, and the data type is integer data.

Import java. SQL .*;

Public class ProcedureNoArgs {
Public static void main (String args []) throws Exception {
// Load the Oracle driver
DriverManager. registerDriver (new oracle. jdbc. driver. OracleDriver ());
// Obtain the Oracle database connection
Connection conn = DriverManager. getConnection (
"Jdbc: oracle: thin :@ MyDbComputerNameOrIP: 1521: ORCL", sUsr, sPwd );
// Create a stored procedure object
CallableStatement c = conn. ppareCall ("{call pro1 (?)} ");
// Set the parameter value for the Oracle stored procedure, and set the value of the first parameter to 188
C. setInt (1,188 );
// Execute the Oracle Stored Procedure
C.exe cute ();
Conn. close ();
}
}

Ii. JAVA calls the Oracle stored procedure [with output parameters]
The process name is pro2, the number of parameters is 2, the data type is integer data, the return value is Integer type

Import java. SQL .*;

Public class ProcedureWithArgs {
Public static void main (String args []) throws Exception {
// Load the Oracle driver
DriverManager. registerDriver (new oracle. jdbc. driver. OracleDriver ());
// Obtain the Oracle database connection
Connection conn = DriverManager. getConnection (
"Jdbc: oracle: thin :@ MyDbComputerNameOrIP: 1521: ORCL", sUsr, sPwd );
// Create an Oracle Stored Procedure object and call the Stored Procedure
CallableStatement c = conn. ppareCall ("{call pro2 (?,?)} ");
// Set the parameter value of the Oracle stored procedure to 188.
C. setInt (1,188 );
// Register the second parameter of the stored procedure
C. registerOutParameter (2, java. SQL. Types. INTEGER );
// Execute the Oracle Stored Procedure
C.exe cute ();
// Obtain and print the output parameter value of the stored procedure
System. out. println (c. getInt (2 ));
Conn. close ();
}
}

The above is the simplest example of calling the Oracle stored procedure in JAVA. Next we will introduce other aspects about the JAVA and Oracle stored procedures.


Ii. Create an Oracle stored procedure using JAVA
Let's take a look at how to use JAVA to compile the Oracle stored procedure. We will write statements in Oracle SQL plus.
I. Start SQL PLUS
Ii. Compile a JAVA class and define a JAVA class name. This stored procedure outputs the Square Value of the input parameter.

Create or replace and compile java source named "PF"
/**
* Create an Oracle stored procedure using the JAVA class
*/
Package org. oraclejava. pro;

Public class javaCreatePro {
Public static String test (int num ){
Return num + ":" + num * num;
}
}

Iii. Create the method test in the JAVA class as an Oracle Function Method

Create or replace function PF_FUN (name integer) return varchar2 as language java name 'org. oraclejava. pro. javaCreatePro. test (java. lang. Integer) return java. lang. string ';


Iv. directly call the created Oracle function on the console

Select PF_FUN (10) from dual;

V. output results
10 square meters: 100

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.