在JDBC API中提供了调用存储过程的方法,通过CallableStatement对象进行操作。CallableStatement对象位于java.sql包中,它继承于PreparedStatement对象,PreparedStatement对象又继承于Statement对象。CallableStatement对象主要用于执行数据库中定义的存储过程和存储函数,其调用方法如下:
调用存储过程:{call
调用存储函数:{?= call
重要方法:
| void | registerOutParameter(String parameterName, int sqlType) 将名为parameterName的OUT参数 parameterName到JDBC类型 sqlType 。 |
|---|---|
基本的步骤:
1、获取CallableStatement 对象 conn.prepareCall(“{ call proc1(?,?)}”); 如果使用的是存储过程sql语句{call procedurename(? ,?, ?)}如果使用的是函数sql语句{ ?=call functionname(? ,?, ?)}
2、给占位符赋值 CallableStatement.set( 位置,值)
3、设置函数或者存储过程的返回值的类型(位置,类型(Types.INTEGER)) CallableStatement.registerOutParameter(int parameterIndex, int sqlType);
4、执行存储过程或函数 CallableStatement.execute(); 也可以使用executeQuery() 和executeUpdate()
5、获取返回的数据 CallableStatement.get(3);
