1. Encapsulation of stored procedures: Right-click Function--New function--select process (process is no return value) or select function (function has return value)
2. Here we select the process-click Next after entering
The mode has in (input parameters), out (outward output parameters), INOUT, where we usually use in and out, if nothing is written the system defaults to in.
After the function is established, we need to encapsulate the command to execute in the function, for example:
Note: To add a semicolon after writing a statement, add the length after the parameter varchar type.
Instance:
PackageCom.inba.maya.cunchu;ImportJava.util.*;ImportJava.sql.*; Public classMycunchu { Public Static voidMain (string[] args)throwsexception{Class.forName ("Com.mysql.jdbc.Driver"); Connection Conn=drivermanager.getconnection ("Jdbc:mysql://localhost:3306/mydb?characterencoding=gbk", "Root", "" "); //Note: When calling the database function, the key word is the call function name must be added (), whether the function is not with parameters, with parameters must write parameters;//called without arguments.//callablestatement Cs=conn.preparecall ("Call TCC ()"); //ResultSet rs=cs.executequery (); //called with parameters.//callablestatement Cs=conn.preparecall ("Call TTA (?,?)"); //Enter two parameters//cs.setstring (1, "k006"); //Cs.setint (2, 90); //ResultSet rs=cs.executequery (); //Output ParametersCallableStatement Cs=conn.preparecall ("Call TDD (?,?)"); //in this case, because I write parameters in the database, the first parameter defined is in, the second argument is out, so the first one is to SetStringCs.setstring (1, "k001"); //because the second one is out, so use Registeroutparameter (position, java.sql.Types. Data type)Cs.registeroutparameter (2, Java.sql.Types.INTEGER); ResultSet RS=Cs.executequery (); //Be sure to complete the query above when you get the parameters intN=cs.getint (2); while(Rs.next ()) {System.out.println (n); System.out.println (Rs.getstring (1) +rs.getstring (2) +rs.getint (5) +rs.getdouble (3)); } conn.close (); }}
Call the MySQL database in Java, the encapsulation and invocation of the data stored procedure;