1. Create a stored procedure
Create a MySQL Stored Procedure add_pro
Copy codeThe Code is as follows:
Delimiter //
Drop procedure add_pro //
Create procedure add_pro (a int, B int, out sum int)
Begin
Set sum = a * B;
End;
//
2. Call the Stored Procedure
Copy codeThe Code is as follows:
Package com. zhanggaosong;
Import java. SQL. CallableStatement;
Import java. SQL. Connection;
Import java. SQL. DriverManager;
Import java. SQL. Types;
Public class CallableStatementTest {
Public static final String DRIVER_CLASS = "com. mysql. jdbc. Driver ";
Public static final String URL = "jdbc: mysql: // FIG: 3306/test ";
Public static final String USERNAME = "root ";
Public static final String PASSWORD = "123456 ";
Public static void main (String [] args) throws Exception {
Class. forName (DRIVER_CLASS );
Connection connection = DriverManager. getConnection (URL, USERNAME,
PASSWORD );
String SQL = "{CALL add_pro (?,?,?)} "; // Call the Stored Procedure
CallableStatement cstm = connection. prepareCall (SQL); // instantiate the object cstm
Cstm. setInt (1,122 );
Cstm. setInt (2, 2 );//
Cstm. registerOutParameter (3, Types. INTEGER); // you can specify the type of the returned value.
Ccmd.exe cute (); // executes the Stored Procedure
System. out. println (cstm. getInt (3 ));
Cstm. close ();
Connection. close ();
}
}