java 调用mysql存储过程

public class Test { 

    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 = "acbee"; 
    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 pro_num_user(?,?)}"; //调用存储过程 
        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.getInt(2)); 
        cstm.close(); 
        connection.close(); 
    } 
     
    public static void test2() throws Exception 
    { 
        Class.forName(DRIVER_CLASS); 
        Connection connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); 
        String sql = "{CALL pro_number(?,?,?)}"; //调用存储过程 
        CallableStatement cstm = connection.prepareCall(sql); //实例化对象cstm 
        cstm.setInt(1, 2); // 存储过程输入参数 
        cstm.setInt(2, 2); // 存储过程输入参数 
        cstm.registerOutParameter(3, Types.INTEGER); // 设置返回值类型 即返回值 
        cstm.execute(); // 执行存储过程 
        System.out.println(cstm.getInt(3)); 
        cstm.close(); 
        connection.close(); 
         
    } 
} 

猜你喜欢

转载自blog.csdn.net/zhuchunyan_aijia/article/details/81143495