Java calls to MySQL stored procedures (simple example)

Source: Internet
Author: User

First I defined a student table in the MySQL test database:

CREATE TABLE Student4 (

ID int PRIMARY KEY,

Sanme Char (5)

);

Insert several data for testing:

Inset into student values (1, ' Mayi ');

Inset into student values (2, ' Mayi ');

Inset into student values (3, ' Mayi ');

To establish a stored procedure:

In type:

Delimiter//

CREATE PROCEDURE demo_in (in p_in int)

Begin

Select P_in;

Set p_in=2;

Select P_in;

End

//

delimiter;

Java Test Code:

CallableStatement callstatement = Conn.preparecall ("Call demo_in (?)"); /conn, database connection


Callstatement.setint (1, 1);//Set the first parameter


Callstatement.execute ();//execution

Conn.close ();//close connection

Out type:

Delimiter//

CREATE PROCEDURE Demo_out (out s int)

Begin

Select COUNT (*) into s from student;

End

//

delimiter;

Java Test Code:

CallableStatement callstatement = Conn.preparecall ("Call Demo_out (?)");


Callstatement.registeroutparameter (1, Java.sql.Types.INTEGER);//The Out-type parameter type for registering stored procedures, which must be registered before use;


Callstatement.execute ();//execution


System.out.println (Callstatement.getint (1));//Get the output parameters of the stored procedure


Conn.close ();//close connection

Java calls to MySQL stored procedures (simple example)

Related Article

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.