Stored procedures are what, their own Baidu, Baidu said better than I said. Why use stored procedures, which can improve efficiency. Say less nonsense, directly on the code:
First, the syntax: write stored procedures in MySQL
DELIMITER $ $CREATE /*[definer = {User | Current_User}]*/ PROCEDURE ' test '. ' Text_d ' (in param1 varchar (ten), in param2 varchar ()) /*language SQL | [NOT] Deterministic | {CONTAINS SQL | NO SQL | READS SQL DATA | Modifies SQL DATA} | SQL SECURITY {definer | INVOKER} | COMMENT ' String ' * /BEGIN insert INTO User (user_name,user_dept) values (PARAM1,PARAM2); end$ $DELIMITER;
The above defines a stored procedure, which is simple enough to be understood, and it is said that the stored procedure is complex.
Here is the Java code:
Package Jdbc_study;import Java.sql.callablestatement;import Java.sql.connection;import java.sql.SQLException; public class Preduce {public static void main (string[] args) {//First get connection to database user user = new user (); User.setusername ("Gohan User.setuserdept ("java direction"), save (user); public static void Save (User user) {//1: Get connection to database connection conn = Basedao.getconnection ();// 2:callablestatementcallablestatement proc = null;try {proc= Conn.preparecall ("{Call Text_c (?,?)}"); Proc.setstring (1, User.getusername ()); Proc.setstring (2, user.getuserdept ()); Proc.execute ();} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Finally {try {proc.close (); Conn.close ()} catch (SQLException e) {//TODO auto-generated catch Blocke.printstacktrace ();} }}}
How Java calls a stored procedure written by MySQL