`
dragonxiangfu
  • 浏览: 156629 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

ADF中调用PLSQL存储过程和函数

 
阅读更多

下面是从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)关闭语句链接

  1. voidcallStoredProcedure(Stringstmt,Object[]bindVars){
  2. PreparedStatementst=null;
  3. try{
  4. //1.CreateaJDBCPreparedStatementfor
  5. st=getDBTransaction().createPreparedStatement("begin"+stmt+";end;",0);
  6. if(bindVars!=null){
  7. //2.Loopovervaluesforthebindvariablespassedin,ifany
  8. for(intz=0;z<bindVars.length;z++){
  9. //3.Setthevalueofeachbindvariableinthestatement
  10. st.setObject(z+1,bindVars[z]);
  11. }
  12. }
  13. //4.Executethestatement
  14. st.executeUpdate();
  15. }
  16. catch(SQLExceptione){
  17. thrownewJboException(e);
  18. }
  19. finally{
  20. if(st!=null){
  21. try{
  22. //5.Closethestatement
  23. st.close();
  24. }
  25. catch(SQLExceptione){}
  26. }
  27. }
  28. }

5,注意点:

1)数据库连接占用数据库资源,务必在finally语句中关闭数据库连接。

2)对于在执行过程中发生的错误要throw出来,否则会增加调试时寻找异常(错误)的难度。

3)执行查询方法execute, executeQuery, executeUpdate的区别:

executeUpdate用于执行带有INSERT/UPDATE/DELETE操作的语句

executeQuery用于执行带有查询操作的语句

execute用于执行任意类型的SQL语句

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics