Java-data storage process (54), java Data Storage
Stored procedure: A piece of executable program. This program runs in data.
Begin = {
End =}
If
Else
You can also receive parameters.
1 define a procedure:
CREATE PROCEDURE proc1()BEGIN ――{ SELECT * FROM users;END // ―― }DELIMITER ;CALL proc1();
2. Define a receiving parameter:
DELIMITER $$CREATE PROCEDURE proc2(IN _id VARCHAR(32),IN _nm VARCHAR(30))BEGIN INSERT INTO users(id,NAME) VALUES(_id,_nm);END $$CALL proc2('U003','Rose');
3. Define a response type parameter:
DELIMITER $$CREATE PROCEDURE proc3(IN _id VARCHAR(32),IN _nm VARCHAR(30),OUT _size INT)BEGIN INSERT INTO users(id,NAME) VALUES(_id,_nm); SELECT COUNT(1) INTO _size FROM users; IF(_size=10) THEN INSERT INTO users VALUES('U0000','ddd','ddddd'); END IF;END $$CALL proc3('U002882','Rose',@wj);SELECT @wj;
Call Procedure in Java code
PackageCn. hx. demo;
ImportJava. SQL. CallableStatement;
ImportJava. SQL. Connection;
ImportJava. SQL. ResultSet;
ImportJava. SQL. Types;
ImportOrg. junit. Test;
ImportCn. itcast. utils. performanceutils;
Public ClassProceDemo {
@ Test
Public VoidProc1 ()ThrowsException {
// Dbutils does not provide the ability to call stored procedures
Connection con = performanceutils.GetDatasSource(). GetConnection ();
// Obtain the object of the call Process
CallableStatement cs =
Con. prepareCall ("{call proc1 ()}");
// Execute
BooleanBoo = cs.exe cute (); // if true is returned, the select statement is executed in the last sentence.
If(Boo ){
ResultSet rs = cs. getResultSet ();
While(Rs. next ()){
System.Err. Println (rs. getString ("name "));
}
}
Con. close ();
}
@ Test
Public VoidProc2 ()ThrowsException {
Connection con = performanceutils.GetDatasSource(). GetConnection ();
// Obtain the object of the call Process
CallableStatement cs =
Con. prepareCall ("{call proc2 (?,?)} ");
Cs. setString (1, "UAAA ");
Cs. setString (2, "11 ");
BooleanBoo = cs.exe cute ();
System.Err. Println (boo );
Con. close ();
}
@ Test
Public VoidProc3 ()ThrowsException {
Connection con = performanceutils.GetDatasSource(). GetConnection ();
// Obtain the object of the call Process
CallableStatement cs =
Con. prepareCall ("{call proc5 (?,?,?)} ");
Cs. setString (1, "UBDDB ");
Cs. setString (2, "Zhang San ");
Cs. registerOutParameter (3, Types.INTEGER); // -- Int,
BooleanBoo = cs.exe cute ();
System.Err. Println (">>:" + boo); // true
// Obtain the returned value from the call.
IntSize = cs. getInt (3 );
System.Err. Println ("number of rows:" + size );
If(Boo ){
ResultSet rs = cs. getResultSet ();
Rs. next ();
IntSs = rs. getInt (1 );
System.Err. Println ("sss:" + ss );
}
Con. close ();
}
@ Test
Public VoidProc6 ()ThrowsException {
Connection con = performanceutils.GetDatasSource(). GetConnection ();
// Obtain the object of the call Process
CallableStatement cs =
Con. prepareCall ("{call proc6 (?,?,?,?)} ");
Cs. setString (1, "UBafadsB ");
Cs. setString (2, "Zhang San ");
Cs. registerOutParameter (3, Types.INTEGER); // -- Int,
Cs. registerOutParameter (4, Types.INTEGER);
BooleanBoo = cs.exe cute ();
System.Err. Println (">>:" + boo); // faluse
// Obtain the returned value from the call.
IntSize = cs. getInt (3 );
Int_ S = cs. getInt (4 );
System.Err. Println ("number of rows:" + size + "," + _ s );
Con. close ();
}
}