JDBC calls Mysql stored procedure implementation code _MYSQL

Source: Internet
Author: User
1. Create a stored procedure
Create a MySQL stored procedure Add_pro
Copy Code code 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 Code code 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://127.0.0.1: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 (?,?,?)}"; Calling stored procedures
CallableStatement cstm = connection.preparecall (sql); Instantiating an object cstm
Cstm.setint (1, 122);
Cstm.setint (2, 2); //
Cstm.registeroutparameter (3, Types.integer); Set return value type
Cstm.execute (); Executing stored procedures
System.out.println (Cstm.getint (3));
Cstm.close ();
Connection.close ();
}
}

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.