在java中调用mysql中的存储过程

    技术2022-07-10  129

    标题:在java中调用mysql中的存储过程

    一、具体操作

    1.获得连接 //获得连接 connection = TestDemoUtilsByDruid.getConnection(); 2.创建存储过程的对象 //创建存储过程的对象 callableStatement=connection.prepareCall("{call test_purchase(?,?,?,?)}"); 3.给存储过程的参数设置值 //给存储过程的参数设置值 callableStatement.setObject(1,idCard); callableStatement.setObject(2,carId); callableStatement.setObject(3,grade); 4.注册存储过程的第four个参数 //注册存储过程的第four个参数 callableStatement.registerOutParameter(4,java.sql.Types.INTEGER); 5.执行存储过程 //执行存储过程 callableStatement.execute(); 6.得到存储过程的输出参数值 //得到存储过程的输出参数值 result = callableStatement.getInt(4);

    二、 示例方法如下:

    public int getOrderTicket(String idCard,String carId,String grade) { int result =0; Connection connection=null; CallableStatement callableStatement=null; try { //获得连接 connection = TestDemoUtilsByDruid.getConnection(); //创建存储过程的对象 callableStatement=connection.prepareCall("{call test_purchase(?,?,?,?)}"); //给存储过程的参数设置值 callableStatement.setObject(1,idCard); callableStatement.setObject(2,carId); callableStatement.setObject(3,grade); //注册存储过程的第four个参数 callableStatement.registerOutParameter(4,java.sql.Types.INTEGER); //执行存储过程 callableStatement.execute(); //得到存储过程的输出参数值 result = callableStatement.getInt(4); return result; } catch (SQLException e) { e.printStackTrace(); }finally { TestDemoUtilsByDruid.getClose(null, callableStatement, connection); } return result; }
    Processed: 0.023, SQL: 9