Java stored procedure call

Source: Internet
Author: User

Create a required test table: Create Table Test (TID varchar2 (10), tname varchar2 (10 ));

First case: no return value.

Create or replace procedure test_a (param1 in varchar2, param2 in varchar2)

Begin

Insert into test value (param1, param2 );

End;

Java call code:

Package com. test;

Import java. SQL .*;

Import java. Io .*;

Import java. SQL .*;

Public class testproca
{
Public testproca (){

}

Public static void main (string [] ARGs)
{

Resultset rs = NULL;
Connection conn = NULL;
Callablestatement proc = NULL;

Try {
Class. forname ("oracle. JDBC. Driver. oracledriver ");
Conn = drivermanager. getconnection ("JDBC: oracle: thin: @ FIG: 1521: Test", "test", "test ");
Proc = conn. preparecall ("{call test_a (?,?) }");
Proc. setstring (1, "1001 ");
Proc. setstring (2, "Testa ");
Proc.exe cute ();
} Catch (exception e ){
E. printstacktrace ();
} Finally {
Try {
If (null! = RS ){
Rs. Close ();

If (null! = Proc ){
Proc. Close ();
}

If (null! = Conn ){
Conn. Close ();
}
}
} Catch (exception ex ){

}
}
}

}

Case 2: stored procedure with a returned value (the returned value is not a list ).

The stored procedure is:
Create or replace procedure test_ B (param1 in varchar2, param2 out varchar2)
As
Begin
Select tname into param2 from test where tid = param1;
End;

Java call code:

Package com. test;

Import java. SQL .*;

Import java. Io .*;

Import java. SQL .*;

Public class testprocb
{
Public testprocb (){

}

Public static void main (string [] ARGs)
{

Connection conn = NULL;
Callablestatement proc = NULL;

Try {
Class. forname ("oracle. JDBC. Driver. oracledriver ");
Conn = drivermanager. getconnection ("JDBC: oracle: thin: @ FIG: 1521: Test", "test", "test ");
Proc = conn. preparecall ("{call test_ B (?,?) }");
Proc. setstring (1, "1001 ");
Proc. registeroutparameter (2, types. varchar );
Proc.exe cute ();
System. Out. println ("output is:" + Proc. getstring (2 ));
} Catch (exception e ){
E. printstacktrace ();
} Finally {
Try {

If (null! = Proc ){
Proc. Close ();
}

If (null! = Conn ){
Conn. Close ();
}
 
} Catch (exception ex ){

}
}
}

}

Case 3: Return to the list.

Since the Oracle stored procedure does not return values, all of its return values are replaced by the out parameter, and the list is no exception. However, because it is a set, general parameters cannot be used, you must use pagkage. it should be written in two parts:
Create or replace package tpackage
Type t_cursor is ref cursor;
Procedure test_c (c_ref out t_cursor );
End;

Create or replace package body tpackage
Procedure test_c (c_ref out t_cursor) is
Begin
Open c_ref for select * from test;
End test_c;
End tpackage;

Java call code:

Package com. test;

Import java. SQL .*;

Import java. Io .*;

Import java. SQL .*;

Public class testprocb
{
Public testprocb (){

}

Public static void main (string [] ARGs)
{

Connection conn = NULL;
Callablestatement proc = NULL;
Resultset rs = NULL;
Try {
Class. forname ("oracle. JDBC. Driver. oracledriver ");
Conn = drivermanager. getconnection ("JDBC: oracle: thin: @ FIG: 1521: Test", "test", "test ");
Proc = conn. preparecall ("{? = Call tpackage. test_ B (?) }");

Proc. registeroutparameter (1, oracletypes. cursor );
Proc.exe cute ();
While (Rs. Next ()){
System. Out. println (Rs. GetObject (1) + "/t" + Rs. GetObject (2 ));
}
} Catch (exception e ){
E. printstacktrace ();
} Finally {
Try {
If (null! = RS ){
Rs. Close ();
If (null! = Proc ){
Proc. Close ();
}

If (null! = Conn ){
Conn. Close ();
}
}
} Catch (exception ex ){

}
}
}

}

 

Hibernate calls OracleStorageProcess

This.pnumbermanager.gethibernatetemplate(cmd.exe cute (

New hibernatecallback ()...{

Public object doinhibernate (session)

Throws hibernateexception, sqlexception ...{

Callablestatement cs = session. Connection (). preparecall ("{call modifyapppnumber_remain (?)} ");

CS. setstring (1, foundationid );

Cs.exe cute ();

Return NULL;

}

});

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.