下面是从ADF guide中总结的一段调用PLSQL存储过程和函数的方法,仅供参考。
1,调用没有参数的存储过程
可以使用executeCommand()函数,在AM的实现类中可以这样来使用:
getDBTransaction().executeCommand("begin devguidepkg.proc_with_no_args; end;");
2,调用只有输入参数的存储过程
可以使用getDBTransaction提供的createPreparedStatement来创建PreparedStatement对象,使用示例:
getDBTransaction().createPreparedStatement("begin "+stmt+";end;",0);
3,调用只有输入参数的存储函数
可以使用getDBTransaction提供的createCallableStatement来创建CallableStatement对象,使用示例:
getDBTransaction().createCallableStatement("begin ? := "+stmt+";end;",0);
4,其他情况
调用既有输入参数又有输出参数的存储过程或函数可以使用CallableStatement,使用方法和3相同。
5,调用存储过程
操作流程:
1)创建PreparedStatement
2)设置输入输出参数
3)执行查询
4)关闭语句链接
- voidcallStoredProcedure(Stringstmt,Object[]bindVars){
- PreparedStatementst=null;
- try{
- //1.CreateaJDBCPreparedStatementfor
- st=getDBTransaction().createPreparedStatement("begin"+stmt+";end;",0);
- if(bindVars!=null){
- //2.Loopovervaluesforthebindvariablespassedin,ifany
- for(intz=0;z<bindVars.length;z++){
- //3.Setthevalueofeachbindvariableinthestatement
- st.setObject(z+1,bindVars[z]);
- }
- }
- //4.Executethestatement
- st.executeUpdate();
- }
- catch(SQLExceptione){
- thrownewJboException(e);
- }
- finally{
- if(st!=null){
- try{
- //5.Closethestatement
- st.close();
- }
- catch(SQLExceptione){}
- }
- }
- }
5,注意点:
1)数据库连接占用数据库资源,务必在finally语句中关闭数据库连接。
2)对于在执行过程中发生的错误要throw出来,否则会增加调试时寻找异常(错误)的难度。
3)执行查询方法execute, executeQuery, executeUpdate的区别:
executeUpdate用于执行带有INSERT/UPDATE/DELETE操作的语句
executeQuery用于执行带有查询操作的语句
execute用于执行任意类型的SQL语句