java操作mysql存储过程示例

数据库 MySQL
java操作mysql存储过程未必人人都知道,下文以示例的形式为您介绍java操作mysql存储过程,供您参考学习。

mysql存储过程每个DBA都需要掌握的,下面就为您介绍java操作mysql存储过程的例子,如果您对mysql存储过程方面感兴趣的话,不妨一看。

1、新建表test

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

2、删除已存在的存储过程
-- 删除储存过程
delimiter // -- 定义结束符号
drop procedure p_test;//

3、mysql存储过程定义

  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、调用方法

  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. }  

以上java操作mysql存储过程示例的介绍。

 

 

 

【编辑推荐】

建立MySQL触发器的语法

配置连接多个mysql服务器的方法

MYSQL BENCHMARK函数的使用

MySQL清除字符串首尾空格函数

MySQL中concat_ws函数的使用

责任编辑:段燃 来源: 互联网
相关推荐

2010-05-26 14:55:43

MySQL存储过程

2010-10-09 17:08:15

MySQL存储过程

2011-08-12 14:01:21

SQL Server存储过程

2010-06-02 16:49:24

MySQL存储过程

2011-08-16 16:59:58

PLSQL分页存储过程Java

2011-08-12 11:22:53

Oracle存储过程Java

2010-09-09 09:49:18

SQL函数存储过程

2020-11-26 10:33:44

MySQL存储函数

2009-08-06 16:18:38

C#调用SQL存储过程

2021-10-15 06:43:11

数据库存储过程

2010-04-12 09:26:52

Oracle查询分页

2011-08-22 10:15:39

数据库存储过程

2010-06-04 14:18:10

MySQL 分页存储过

2010-05-13 15:54:56

MySQL分页查询

2020-11-02 13:24:49

MySQL数据库存储

2009-09-17 10:40:23

linq存储过程

2010-05-19 11:25:46

MySQL触发器

2019-01-02 13:03:53

MySQL存储权限

2010-06-01 15:09:55

MySQL 存储过程

2010-05-19 14:03:41

MySQL 存储过程
点赞
收藏

51CTO技术栈公众号