JAVA 调用mysql存储过程

public class Test {
//连接mysql数据库
public static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
public static final String URL = "jdbc:mysql://localhost/bailv_case";
public static final String USERNAME = "root";
public static final String PASSWORD = "root";
public static void main(String[] args) throws Exception {
test1();
test2();
} public static void test1() throws Exception
{
Class.forName(DRIVER_CLASS);
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
String sql = "{CALL gq_hs()}"; //调用存储过程CALL gq_hs()
        CallableStatement cstm = connection.prepareCall(sql); //实例化对象cstm
// cstm.setString(1, "myd"); //存储过程输入参数
//cstm.setInt(2, 2); // 存储过程输入参数
// cstm.registerOutParameter(2, Types.INTEGER); // 设置返回值类型 即返回值
cstm.execute(); // 执行存储过程
System.out.println("成功");
cstm.close();
connection.close();
} public static void test2() throws Exception
{
Class.forName(DRIVER_CLASS);
Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
String sql = "{CALL out_in(?,?)}"; //调用存储过程CALL out_in(?,?)有参
        CallableStatement cstm = connection.prepareCall(sql); //实例化对象cstm 
cstm.setInt(1,2); // 存储过程输入参数
// cstm.setInt(2,2); // 存储过程输入参数
cstm.registerOutParameter(2, Types.INTEGER); // 设置返回值类型 即返回值
cstm.execute(); // 执行存储过程
System.out.println("成功"+cstm.getInt(2));
cstm.close();
connection.close(); }
上一篇:AJAX原理及XMLHttpRequest对象分析


下一篇:阿里宣布Atlas正式开源:带你重返App开发的田园时代