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

转载自  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. 修改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. 将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  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值