CallableStatement cStmt = conn. prepareCall ("{call p_test ()}"); CStmt.exe cuteUpdate (); Import java. SQL .*; /** IGoder */ Public class ProcedureTest { /* The table and stored procedure are defined as follows: Delimiter // Drop table if exists test // Create table test ( Id int (11) NULL )// Drop procedure if existssp1 // Create procedure sp1 (in p int) Comment 'insert into a int value' Begin Declare v1 int; Set v1 = p; Insert into test (id) values (v1 ); End // Drop procedure if exists sp2 // Create procedure sp2 (out p int) Begin Select max (id) into p from test; End // Drop procedure if exists sp6 // Create procedure sp6 () Begin Select * from test; End // */ Public static void main (String [] args ){ // Call in (111 ); // CallOut (); CallResult (); } /** * Call a stored procedure with input parameters * @ Param in stored procedure input parametervalue */ Public static void callIn (int in ){ // Obtain the connection Connection conn = ConnectDb. getConnection (); CallableStatement cs = null; Try { // You can directly input parameters. // Cs = conn. prepareCall ("{call sp1 (1 )}"); // You can also use question marks instead. Cs = conn. prepareCall ("{call sp1 (?)} "); // Set the value of the first input parameter to 110. Cs. setInt (1, in ); Cs.exe cute (); } Catch (Exception e ){ E. printStackTrace (); } Finally { Try { If (cs! = Null ){ Cs. close (); } If (conn! = Null ){ Conn. close (); } } Catch (Exception ex ){ Ex. printStackTrace (); } } } /** * Call a stored procedure with output parameters * */ Public static void callOut (){ Connection conn = ConnectDb. getConnection (); CallableStatement cs = null; Try { Cs = conn. prepareCall ("{call sp2 (?)} "); // The type of the first parameter is Int. Cs. registerOutParameter (1, Types. INTEGER ); Cs.exe cute (); // Obtain the first value 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 (); } } } /** * Call the stored procedure of the output result set */ Public static void callResult (){ Connection conn = ConnectDb. getConnection (); CallableStatement cs = null; ResultSet rs = null; Try { Cs = conn. prepareCall ("{call sp6 ()}"); Rs = cs.exe cuteQuery (); // Cyclically output results 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 (); } } } } /** * Obtain the database connection class */ Import java. SQL. Connection; Import java. SQL. DriverManager; Import java. SQL. PreparedStatement; Import java. SQL. ResultSet; Import java. SQL. SQLException; Import java. SQL. Statement; 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 = 8859_1 "; Conn = DriverManager. getConnection (url ); } Catch (Exception e ){ E. printStackTrace (); } Return conn; } } |