Example of a java mysql Stored Procedure

Source: Internet
Author: User

Every DBA of the mysql stored procedure needs to master. The following describes examples of java operations on the mysql stored procedure. If you are interested in the mysql stored procedure, take a look.

1. Create a table named test

 
 
  1. create table test(  
  2. field1 int not null  
  3. )  
  4. TYPE=MyISAM ;  
  5. insert into test(field1) values(1); 

2. delete an existing Stored Procedure
-- Delete the Stored Procedure
Delimiter // -- Define the end symbol
Drop procedure p_test ;//

3. mysql Stored Procedure Definition

 
 
  1. create procedure p_test()  
  2. begin  
  3. declare temp int;  
  4. set temp = 0;   
  5. update test set field1 =temp;  
  6. end  
  7. // 

4. Call Method

 
 
  1. call p_test();  
  2. import java.sql.*;  
  3.  
  4. public class Test Conn{  
  5.  
  6. private Connection getConn(){  
  7. Connection conn = null;  
  8. try {  
  9.    Class.forName("org.gjt.mm.mysql.Driver");  
  10.    try {  
  11.     conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&  
  12.     characterEncoding=GBK","root","ntsky");  
  13.    } catch (SQLException e1) {  
  14.     e1.printStackTrace();  
  15.    }  
  16. }  
  17. catch (ClassNotFoundException e) {  
  18.    e.printStackTrace();  
  19. }  
  20. return conn;   
  21. }  
  22.  
  23. public void testC() {  
  24. Connection conn = getConn();  
  25. ResultSet rs = null;  
  26. CallableStatement cs = null;  
  27. String a = null;  
  28. try {  
  29.    CallableStatement cStmt = conn.prepareCall("{call p_test()}");  
  30.    cStmt.executeUpdate();  
  31. } catch (Exception e) {  
  32.    System.out.println("hahad" + e.getMessage());  
  33. } finally {  
  34.    try {  
  35.     conn.close();  
  36.    } catch (Exception ex) {  
  37.     System.out.println("ex : " + ex.getMessage());  
  38.    }  
  39. }  
  40.  
  41. }  
  42.  
  43. public static void main(String[] args) {  
  44. new TestConn().testC();  
  45. }  
  46. }  

The preceding example describes how to use java to operate mysql stored procedures.

Create MySQL trigger syntax

How to configure connection to multiple mysql servers

Use of mysql benchmark Function

MySQL clears the space at the beginning and end of a string.

Use of the concat_ws function in 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.