jdbc调用mysql存储过程实现代码带有输入和输出

转载自 http://www.jb51.net/article/34747.htm

1. 创建存储过程 
建立一个MySQL的存储过程 add_pro 

复制代码代码如下:

delimiter // 
drop procedure add_pro // 
create procedure add_pro(a int , b int , out sum int ) 
begin 
set sum = a * b; 
end; 
// 


2. 调用存储过程 

复制代码代码如下:

package com.zhanggaosong; 
import java.sql.CallableStatement; 
import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.Types; 
public class CallableStatementTest { 
public static final String DRIVER_CLASS = "com.mysql.jdbc.Driver"; 
public static final String URL = "jdbc:mysql://127.0.0.1:3306/test"; 
public static final String USERNAME = "root"; 
public static final String PASSWORD = "123456"; 
public static void main(String[] args) throws Exception { 
Class.forName(DRIVER_CLASS); 
Connection connection = DriverManager.getConnection(URL, USERNAME, 
PASSWORD); 
String sql = "{CALL add_pro(?,?,?)}"; //调用存储过程 
CallableStatement cstm = connection.prepareCall(sql); //实例化对象cstm 
cstm.setInt(1, 122); 
cstm.setInt(2, 2); // 
cstm.registerOutParameter(3, Types.INTEGER); // 设置返回值类型 
cstm.execute(); // 执行存储过程 
System.out.println(cstm.getInt(3)); 
cstm.close(); 
connection.close(); 

posted @ 2016-05-26 20:05  testway  阅读(2245)  评论(0编辑  收藏  举报