4. Stored Procedure Call
Replace the preceding Statement or PreparedStatement with java. SQL. CallabelStatement.
Interface used to execute SQL stored procedures. Jdbc api provides a Stored Procedure SQL escape syntax that allows all RDBMS to call stored procedures in a standard way. This escape syntax has a form that contains result parameters and a form that does not contain result parameters. If you use result parameters, you must register them as OUT parameters. Other parameters can be used for input, output, or both. Parameters are referenced in sequence by number. The first parameter is numbered 1.
{? = Call <procedure-name> [<arg1>, <arg2>,...]} stored procedures with returned states
{Call <procedure-name> [<arg1>, <arg2>,...]} stored procedures with or without return values
The IN parameter value is set using the set method inherited from PreparedStatement. Before executing a stored procedure, you must register the types of all OUT parameters. Their values are retrieved using the get method provided by this class after execution.
General steps:
4.1 call the prepareCall (String SQL) method through the java. SQL. Connection instance, and use parameterized SQL statements as parameters to create the CallableStatement object.
4.2 Use the java. SQL. CallableStatement instance to call the setXXX () method and set the corresponding parameter value. In fact, the setXXX () method is inherited from PreparedStatement.
4.3 Use the java. SQL. CallableStatement instance to call methods such as executeQuery () and executeUpdate () to perform database query and update operations.
Key code:
String username = "admin ";
String password = "admin ";
String SQL = "{call login_check (?,?)} ";
Db. cstmt = db. conn. prepareCall (SQL );
Db. cstmt. setString (1, username );
Db. cstmt. setString (2, password );
ResultSet rs = db.cstmt.exe cuteQuery ();