Jdbc calls the mysql stored procedure implementation code _ MySQL

Source: Internet
Author: User
Jdbc calls the mysql stored procedure implementation code bitsCN.com 1. create a stored procedure
Create a MySQL stored procedure add_pro

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

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 ();
}
}
BitsCN.com

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.