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