Java calls stored procedures and functions

Source: Internet
Author: User

In order to increase, delete, change, check the table test to explain:
1. Create a new table test
CREATE TABLE TEST
(
TID number is not NULL,
Tname VARCHAR2 (32),
Tcode VARCHAR2 (32),
CreateDate DATE
)
ALTER TABLE TEST
Add constraint pk_test_id primary key (TID)
2. Scripts for stored procedures in the database:
Create or Replace package test_package is
Procedure Test_add (PID number,pcode varchar2,pname varchar2,pcreatedate date);
Procedure test_update (PID number,pname varchar2);
Procedure Getnameandcodebyid (PID number,pcode out varchar2,pname out varchar2);
function Getnamebyid (PID number) return varchar2;
End Test_package;

Create or Replace package body test_package is
/*
Add a piece of data to the table
*/
Procedure Test_add (PID Number,pcode Varc Har2,pname varchar2,pcreatedate date) is
begin
inserts into test values (pid,pcode,pname,pcreatedate);
Commit ;
End;
/*
Tname of data in the table with TID as PID is updated to PName
*/
Procedure test_update (PID number,pname varchar2) is
begin
Update test set tname=pname where Tid=pid;
Commit;
End;
/*
to find the corresponding code and name according to the ID
*/
Procedure Getnameandcodebyid (PID number,pcode out varchar2,pname out varchar2) Is
begin
Select T.tcode,t.tname to pcode,pname from Test t where T.tid=pid;
End;
/*
Find corresponding NAME
*/
function Getnamebyid (PID number) return varchar2 is
returnvalue varchar2 (+): = ';
Begin
Select T.tn Ame into returnvalue from Test t where T.tid=pid;
Return returnvalue;
End;
End Test_package;

3. Calling a stored procedure in a Java program
A. Adding a piece of data to a table through a stored procedure
public void Add () throws Exception
{
Connection con=new condb (). getconnection ();
String sql= "{call Test_package.test_add (?,?,?,?)}";
SQL can also be written as "Begin Test_package.test_add (?,?,?,?); End; "

CallableStatement stmt=con.preparecall (SQL);
Stmt.setint (1, 1);
Stmt.setstring (2, "test");
Stmt.setstring (3, "test");
Note the handling of date types
Stmt.setdate (4, New Date (New Java.util.Date (). GetTime ()));

Stmt.execute ();
Stmt.close ();
Con.close ();
}

B. Update the Tname field of this data with a given TID
public void Update () throws Exception
{
Connection con=new condb (). getconnection ();
String sql= "{call Test_package.test_update (?,?)}";

CallableStatement stmt=con.preparecall (SQL);
Stmt.setint (1, 1);
Stmt.setstring (2, "Update");
Stmt.execute ();
Stmt.close ();
Con.close ();
}

C. Calling a stored procedure returns tcode and Tname based on a given TID

public void Getnameandcodebyid () throws Exception
{
String tcode= "";
String tname= "";
Connection con=new condb (). getconnection ();

String sql= "{call Test_package.getnameandcodebyid (?,?,?)}";
CallableStatement stmt=con.preparecall (SQL);
Stmt.setint (1, 1);
Stmt.registeroutparameter (2, Types.varchar);
Stmt.registeroutparameter (3, Types.varchar);

Stmt.execute ();
Tname=stmt.getstring (2);
Tcode=stmt.getstring (3);
System.out.println ("Tname=" +tname+ ", tcode=" +tcode);
Stmt.close ();
Con.close ();
}

D. Returning Tname based on a given TID by calling the function
public void Getnamebyid () throws Exception
{
String tname= "";
Connection con=new condb (). getconnection ();
String sql= "Select Test_package.getnamebyid (?) from dual";

CallableStatement stmt=con.preparecall (SQL);
Stmt.setint (1, 1);
ResultSet Rs=stmt.executequery ();
while (Rs.next ())
{
Tname=rs.getstring (1);
}
System.out.println ("name=" +tname);
Stmt.close ();
Con.close ();
}

Java calls stored procedures and functions

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.