How Java calls a stored procedure written by MySQL

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.