Java implementation calls the MySQL stored procedure parsing code

Source: Internet
Author: User

the test code for the database is as follows:

1. The new table Test,sql code is as follows:

CREATE TABLE test (field1 int not null) Type=myisam, insert INTO Test (field1) values (1);


2, delete the existing stored procedures, the code is as follows:

Delimiter//--Define End symbol drop procedure p_test;


3, the MySQL stored procedure definition, the code is as follows:

CREATE PROCEDURE p_test () begindeclare temp Int;set temp = 0; Update Test Set field1 = VALUES (temp); end


4. Java calls the stored procedure with the input parameters, the code is as follows:

public static void Callin (int in) {//get connection Connection conn = Connectdb.getconnection (); CallableStatement cs = null;  try {//can pass directly to the parameter//cs = Conn.preparecall ("{Call SP1 (1)}");  You can also use a question mark instead of CS = Conn.preparecall ("{Call SP1 (?)}");  Set the value of the first input parameter to Cs.setint (1, in); Cs.execute ();  } catch (Exception e) {e.printstacktrace ();} finally {try {if (cs! = null) {cs.close ();  } if (conn! = null) {conn.close ();  }} catch (Exception ex) {ex.printstacktrace (); } } }


5. Java calls a stored procedure with an output parameter, the code is as follows:

public static void CallOut () {Connection conn = connectdb.getconnection (); CallableStatement cs = null;  try {cs = Conn.preparecall ("{Call SP2 (?)}");  The first parameter is of type int cs.registeroutparameter (1, Types.integer);  Cs.execute ();  Gets the first value of int i = Cs.getint (1); System.out.println (i);  } catch (Exception e) {e.printstacktrace ();} finally {try {if (cs! = null) {cs.close ();  } if (conn! = null) {conn.close ();  }} catch (Exception ex) {ex.printstacktrace (); } } }


6, Java calls the output result set stored procedures, the code is as follows:

public static void Callresult () {Connection conn = connectdb.getconnection (); CallableStatement cs = null; ResultSet rs = null;  try {cs = Conn.preparecall ("{Call SP6 ()}");  rs = Cs.executequery ();  The loop outputs the result while (Rs.next ()) {System.out.println (rs.getstring (1));  }} catch (Exception e) {e.printstacktrace ();} finally {try {if (rs! = null) {rs.close ();  } if (cs! = null) {cs.close ();  } if (conn! = null) {conn.close ();  }} catch (Exception ex) {ex.printstacktrace (); } } }}


7, Java to get the database connection tool class, the code is as follows:

Import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.preparedstatement;import Java.sql.resultset;import Java.sql.sqlexception;import java.sql.Statement; public class Connectdb {public static Connection getconnection () {Connection conn = null; PreparedStatement preparedstatement = null;   try {class.forname ("Org.gjt.mm.mysql.Driver"). newinstance ();  String dbname = "Test"; String url= "jdbc:mysql://localhost/dbname?user=root&password=root&useunicode=true&characterencoding  =utf-8 "; conn= drivermanager.getconnection (URL);  } catch (Exception e) {e.printstacktrace ();} Return conn; } }

Java implementation calls the MySQL stored procedure parsing code

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.