JDBC--调用函数&存错过程
1、通过Connection对象的prepareCall()方法创建CallableStatement对象的实例,在使用prepareCall()方法时需传入一个String类型的字符串,该字符串用于指明如何调用存储过程;
--{?= call function_name[(arg0, arg1, ... , argn)]}
--{call procedure_name[(arg0, arg1, ... , argn)]}
2、通过CallableStatement对象的registerOutParameter()方法注册OUT参数;
3、通过CallableStatement对象的setXxx()方法设定 IN 或 IN OUT 参数,若想将参数默认值设为null,可以使用setNull()方法;
4、通过CallableStatement对象的execute()方法执行存错过程;
5、若所调用的是带返回参数的存储过程,还需要通过CallableStatement对象的 getXxx() 方法获取其返回值.
public void testCallableStatement(){ Connection conn = null; CallableStatement callableStatement = null; try{ conn = JDBCUtils.getConnection(); String sql = "{?= call add_a_b(?, ?, ?)}"; callableStatement = conn.prepareCall(sql); callableStatement.registerOutParameter(1, Types.NUMERIC); callableStatement.registerOutParameter(4, Types.NUMERIC); callableStatement.setInt(2, 23); callableStatement.setInt(3, 56); callableStatement.execute(); int ans = callableStatement.getInt(1); System.out.println(ans); ans = callableStatement.getInt(4); System.out.println(ans); }catch(Exception e){ e.printStackTrace(); }finally{ JDBCUtils.release(conn, callableStatement, null); } }