JAVA calls the stored procedure by calling database functions

Source: Internet
Author: User

The following describes how to call a stored procedure through a database function:

Create database function stored procedures

Create or replace function stu_proc
(
V_id IN NUMBER
)
RETURN VARCHAR2 IS
V_name VARCHAR2 (20 );
BEGIN
SELECT o. sname INTO v_name FROM student o WHERE o. id = v_id;
RETURN v_name;
EXCEPTION
When others then return 'data not found ';
END;

JAVA calls the stored procedure by calling database functions

Package com. ljq. test;

Import java. SQL. CallableStatement;
Import java. SQL. Connection;
Import java. SQL. SQLException;
Import java. SQL. Types;

Public class ProceTest {

Public static void main (String [] args) throws Exception {
Connection conn = null;
CallableStatement statement = null;
//? The return Value of the function. stu_proc is the database function name.
// Both the out and in parameters of the stored procedure are passed in. This is one of the differences between the function and the stored procedure.
String SQL = "{? = Call stu_proc (?)} ";
Try {
Conn = ConnUtils. getConnection ();
Statement = conn. prepareCall (SQL );
Statement. registerOutParameter (1, Types. VARCHAR );
Statement. setInt (2, 36 );
Statement.exe cute ();
// The specific value or data is not found
String msg = statement. getString (1 );
System. out. println (msg );
} Catch (SQLException e ){
E. printStackTrace ();
} Finally {
ConnUtils. free (null, statement, conn );
}
}

}

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.