MySQL database stored procedures

What is a stored procedure? ——Stored procedure (Stored Procedure) is a set of SQL statements in a large database system to complete a specific function, which is stored in the database. After the first compilation, it does not need to be recompiled again. name and give parameters (if the stored procedure has parameters) to execute it.
A stored procedure is equivalent to a whole. In a word, multiple SQL statements can be executed at the same time with one call. 
 

Step 1: Create a stored procedure by writing SQL command statements :
 
1. Create a stored procedure
DROP PROCEDURE IF EXISTS usp_add; --  usp_add is a stored procedure name defined
DELIMITER &&  --   The mysql stored procedure ends automatically when it encounters a semicolon. Here we declare that the start and end are represented by the symbol &&, and will not end when a semicolon is encountered.
CREATE PROCEDURE usp_add(IN v_n1  VARCHAR(20) ,IN v_n2  VARCHAR(20) ,OUT o_result  INT ) -- parameter setting: in means input parameter, out means output parameter
BEGIN  -- start of stored procedure
DECLARE t_error INTEGER DEFAULT 0;  # Set the initial value of the error number variable to 0
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error = 1; # If there is any error (not NOT FOUND ), set t_error to 1, and continue to execute
START TRANSACTION;  # define transaction start
INSERT INTO  grade  VALUES(NULL,v_n1); --  grade is the table name
INSERT INTO  grade   VALUES(NULL,v_n2);
# Determine whether the transaction is committed or rolled back
IF t_error = 1 THEN
     SET o_result=0;
     ROLLBACK;  # rollback
ELSE
     SET o_result=1;
     COMMIT;  # Commit
END IF;  
END  -- the end of the stored procedure
&&  -- End with && symbol
DELIMITER ; -- restore the original initial state, there needs to be a space before the semicolon

2. Call the stored procedure
CALL usp_add(' Grade 1 ', 'Grade 2 ',@o_result); -- Pass in 2 parameters and output a result

 3. Query the results returned by the stored procedure
SELECT @o_result; Step 2: Call the written stored procedure in the Java class:


 

import java.sql.CallableStatement;
import java.sql.Connection;
 
public boolean addGrade( String n1, String n2 ) {
     Connection conn=null;
     boolean flag=false;
     try {
         conn=getConn();  //connect to database
        //Create a call stored procedure object
        String sql="
{call usp_add(?,?,?)} ";  
         CallableStatement c=conn.prepareCall( sql );
         c.set String (1,  n1 );
        c. set String(2,  n2);
        c.registerOutParameter(3, java.sql.Types.INTEGER); //接收返回值
        c.execute();
        String result=c.getInt(3);
        if( result==1){  //返回的结果为1时,表示插入数据成功。
            flag=true;
        }else{
            flag=false;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }finally{
        closeAll(conn, null,null);  //关闭数据库
    }
return flag;
}
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325433433&siteId=291194637