Java database connection-stored procedure call

Source: Internet
Author: User

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

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.