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 );
}
}
}