MySQl存储过程学习及其在Java中的调用

news/2024/5/18 23:11:32 标签: Java, MySQL, SQL, JDBC, Blog

转载自  http://yoyo08.iteye.com/blog/474915

 

使用存储过程的好处就不说了,下面简要说一下存储过程的使用。

 

1. 首先,创建一个新表,用于后面Java代码中的测试。

 

Java代码 复制代码
  1. create table a    
  2. (   
  3.     id int(10) not null,   
  4.     name varchar(20) not null  
  5.   
  6. )ENGINE=MyISAM DEFAULT CHARSET=utf8;  
create table a 
(
	id int(10) not null,
	name varchar(20) not null

)ENGINE=MyISAM DEFAULT CHARSET=utf8;

 

2. 向表中插入一些数据。

 

Java代码 复制代码
  1. insert into a ( `id`, `name` ) values ('1''best');   
  2. insert into a ( `id`, `name` ) values ('2''great');   
  3. insert into a ( `id`, `name` ) values ('3''china');   
  4. insert into a ( `id`, `name` ) values ('4''beijing');  
insert into a ( `id`, `name` ) values ('1', 'best');
insert into a ( `id`, `name` ) values ('2', 'great');
insert into a ( `id`, `name` ) values ('3', 'china');
insert into a ( `id`, `name` ) values ('4', 'beijing');

 

3. 修改SQL.html" title=MySQL>MySQL中的默认换行符";"为'##'。

 

 

Java代码 复制代码
  1. delimiter ##  
delimiter ##

 

4. 创建一个带有一个输出参数的存储过程。

 

Java代码 复制代码
  1. create procedure hello(out num int)   
  2. begin   
  3. select Max(id) into num from a;   
  4. end##  
create procedure hello(out num int)
begin
select Max(id) into num from a;
end##

 

5. 创建一个带有两个输出参数的存储过程。

 

Java代码 复制代码
  1. create procedure hello2(out num int, out str varchar(20))   
  2. begin   
  3. select Max(id) into num from a;   
  4. select name from a where a.id = num into str;   
  5. end##  
create procedure hello2(out num int, out str varchar(20))
begin
select Max(id) into num from a;
select name from a where a.id = num into str;
end##

 

6. 将SQL.html" title=MySQL>MySQL的换行符修改回";"。

 

Java代码 复制代码
  1. delimiter ;  
delimiter ;

 

7. Java程序中的调用。

 

Java代码 复制代码
  1. package cn.lifx.util.procedure;   
  2.   
  3. import java.sql.CallableStatement;   
  4. import java.sql.Connection;   
  5. import java.sql.DriverManager;   
  6. import java.sql.Types;   
  7.   
  8. public class Test    
  9. {   
  10.     String url = "jdbc:mysql://127.0.0.1:3306/test";    
  11.     String name = "root";   
  12.     String password = "china";   
  13.        
  14.     public static void main(String[] args)   
  15.     {   
  16.         Test test = new Test();   
  17.            
  18.         test.proc();   
  19.            
  20.         test.proc2();   
  21.     }   
  22.        
  23.     public Connection getConnection()    
  24.     {   
  25.         Connection con = null;   
  26.            
  27.         try  
  28.         {   
  29.             Class.forName("com.mysql.jdbc.Driver");   
  30.                
  31.             con = DriverManager.getConnection(url, name, password);   
  32.                
  33.         }catch(Exception e){    
  34.             e.printStackTrace();   
  35.         }   
  36.            
  37.         return con;   
  38.     }   
  39.        
  40.     public void proc()   
  41.     {   
  42.         Connection conn = getConnection();   
  43.         CallableStatement stmt = null;   
  44.            
  45.         try  
  46.         {   
  47.             stmt = conn.prepareCall("{call hello(?)}");       
  48.             stmt.registerOutParameter(1, Types.INTEGER);   
  49.             stmt.execute();   
  50.                
  51.             int i = stmt.getInt(1);   
  52.                
  53.             System.out.println(i);   
  54.                
  55.         }catch(Exception e){   
  56.             e.printStackTrace();   
  57.                
  58.         }finally  
  59.         {   
  60.             try    
  61.             {   
  62.                 stmt.close();   
  63.                 conn.close();   
  64.                    
  65.             }catch (Exception e) {   
  66.                 e.printStackTrace();   
  67.             }   
  68.         }   
  69.     }   
  70.        
  71.     public void proc2()   
  72.     {   
  73.         Connection conn = getConnection();   
  74.         CallableStatement stmt = null;   
  75.            
  76.         try  
  77.         {   
  78.             stmt = conn.prepareCall("{call hello2(?, ?)}");       
  79.             stmt.registerOutParameter(1, Types.INTEGER);   
  80.             stmt.registerOutParameter(2, Types.VARCHAR);   
  81.             stmt.execute();   
  82.                
  83.             int i = stmt.getInt(1);   
  84.             String str = stmt.getString(2);   
  85.                
  86.             System.out.println(i + " " + str);   
  87.                
  88.         }catch(Exception e){   
  89.             e.printStackTrace();   
  90.                
  91.         }finally  
  92.         {   
  93.             try    
  94.             {   
  95.                 stmt.close();   
  96.                 conn.close();   
  97.                    
  98.             }catch (Exception e) {   
  99.                 e.printStackTrace();   
  100.             }   
  101.         }   
  102.     }   
  103. }     
package cn.lifx.util.procedure;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Types;

public class Test 
{
	String url = "jdbc:mysql://127.0.0.1:3306/test"; 
    String name = "root";
    String password = "china";
    
    public static void main(String[] args)
    {
        Test test = new Test();
        
        test.proc();
        
        test.proc2();
    }
    
    public Connection getConnection() 
    {
        Connection con = null;
        
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            
            con = DriverManager.getConnection(url, name, password);
            
        }catch(Exception e){ 
        	e.printStackTrace();
        }
        
        return con;
    }
    
    public void proc()
    {
        Connection conn = getConnection();
        CallableStatement stmt = null;
        
        try
        {
            stmt = conn.prepareCall("{call hello(?)}");    
            stmt.registerOutParameter(1, Types.INTEGER);
            stmt.execute();
            
            int i = stmt.getInt(1);
            
            System.out.println(i);
            
        }catch(Exception e){
            e.printStackTrace();
            
        }finally
        {
            try 
            {
                stmt.close();
                conn.close();
                
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    public void proc2()
    {
        Connection conn = getConnection();
        CallableStatement stmt = null;
        
        try
        {
            stmt = conn.prepareCall("{call hello2(?, ?)}");    
            stmt.registerOutParameter(1, Types.INTEGER);
            stmt.registerOutParameter(2, Types.VARCHAR);
            stmt.execute();
            
            int i = stmt.getInt(1);
            String str = stmt.getString(2);
            
            System.out.println(i + " " + str);
            
        }catch(Exception e){
            e.printStackTrace();
            
        }finally
        {
            try 
            {
                stmt.close();
                conn.close();
                
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}	

 

8. 输出结果为:

 

Java代码 复制代码
  1. 4  
  2. 4 beijing  

http://www.niftyadmin.cn/n/873493.html

相关文章

Go语言之常量

欢迎关注,订阅,评论,共同学习,共同进步! 灰子学技术: ​ 一、Go语言中const常量 Go语言的常量关键字是const,用于存储不会改变的数值,定义的格式为: const valueNam…

Linux下免安装qt,Linux打包免安装的Qt程序

本文介绍如何打包Qt程序,使其在没有安装Qt的系统可以运行。默认前提:另外一个系统和本系统是同一个系统版本。1,编写导出依赖包的脚本copylib.sh#!/bin/bashLibDir$PWD"/lib"Target$1lib_array($(ldd $Target | grep -o "/.*&…

mysql批处理之getUpdateCounts()的使用

mysql批处理之getUpdateCounts()的使用 关键字: mysql 这几天,做项目时,为了提高数据入库的速率,就使用addBatch()进行批处理。 当然,先要把Connection设置为setAutoCommit(false) 然后Statement添加addBatch(sql语句) 接着State…

linux动态防火墙,利用 FirewallD 构建动态防火墙

永久禁用区域中的ICMP阻塞firewall-cmd --permanent [--zone] --remove-icmp-block查询区域中的ICMP永久状态firewall-cmd --permanent [--zone] --query-icmp-block如果服务启用,此命令将有返回值。此命令没有输出信息。例: 阻塞公共区域中的响应应答报文:firewall…

selectutil html

转载自&#xff1a; http://wallimn.iteye.com/blog/475499 Html代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns"http://www.w3.org/19…

linux passwd文件格式6,6.用户和组(useradd,passwd)

安全3AAuthentication&#xff1a;认证Authorization&#xff1a;授权Accouting|Audition&#xff1a;审计用户user令牌token,identityLinux用户&#xff1a;Username/UID管理员&#xff1a;root, 0普通用户&#xff1a;1-60000 自动分配系统用户&#xff1a;1-499, 1-999 (Cen…

数据库设计的14个技巧

http://tdjava.iteye.com/blog/477783 下述十四个技巧&#xff0c;是许多人在大量的数据库分析与设计实践中&#xff0c;逐步总结出来的。对于这些经验的运用&#xff0c;读者不能生帮硬套&#xff0c;死记硬背&#xff0c;而要消化理解&#xff0c;实事求是&#xff0c;灵活掌…

JAVA中字符串连接效率的测试

http://xumiao900.iteye.com/blog/477778 JAVA中字符串连接效率的测试 关键字: java中字符串连接效率的测试 比较JAVA中String ,StringBuffer,SrtingBuilder三个对象连接字符串的效率。 我们经常都听有经验的人说&#xff0c;避免使用String通过“”连接字符串&#xff0c;特…