在应用程序中调用Oracle存储过程和存储函数

前端之家收集整理的这篇文章主要介绍了在应用程序中调用Oracle存储过程和存储函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

一 概述

本篇介绍用java访问存储过程和访问存储函数
二 开发环境搭建
1、安装oracle时候,会带有jdbc的驱动程序,存放路径如下:
D:\app\lenovo\product\11.2.0\dbhome_1\jdbc\lib
2、拷贝ojdbc6.jar到eclipse的工程目录的lib目录下。
F:\java\workspace\TestOracle\lib
3、在eclipse工程中选中ojdbc6.jar,右键Build Path,将该jar包加到开发环境中。
4、环境搭建后截图如下:


三 创建JDBC工具类
代码如下:
  1. @H_404_56@package demo.utils;
  2. @H_404_56@import java.sql.Connection;
  3. @H_404_56@import java.sql.DriverManager;
  4. @H_404_56@import java.sql.ResultSet;
  5. @H_404_56@import java.sql.sqlException;
  6. @H_404_56@import java.sql.Statement;
  7. @H_404_56@public@H_404_56@classJDBCUtils{
  8. @H_404_56@private@H_404_56@staticString driver ="oracle.jdbc.OracleDriver";
  9. @H_404_56@private@H_404_56@staticString url ="jdbc:oracle:thin:@127.0.0.1:1521:orcl";
  10. @H_404_56@private@H_404_56@staticString user ="scott";
  11. @H_404_56@private@H_404_56@staticString password ="tiger";
  12. //注册数据库的驱动
  13. @H_404_56@static
  14. {
  15. @H_404_56@try{
  16. Class.forName(driver);
  17. //DriverManager.registerDriver(driver);
  18. }@H_404_56@catch(ClassNotFoundException e){
  19. @H_404_56@throw@H_404_56@newExceptionInInitializerError(e);
  20. }
  21. }
  22. //获得数据库连接
  23. @H_404_56@public@H_404_56@staticConnection getConnection(){
  24. @H_404_56@try{
  25. @H_404_56@returnDriverManager.getConnection(url, user, password);
  26. }@H_404_56@catch(sqlException e){
  27. // TODO Auto-generated catch block
  28. e.printStackTrace();
  29. }
  30. @H_404_56@return@H_404_56@null;
  31. //释放数据库的资源
  32. @H_404_56@public@H_404_56@static@H_404_56@void release(Connection conn,Statement st,ResultSet rs){
  33. @H_404_56@if(rs!=@H_404_56@null){
  34. rs.close();
  35. }@H_404_56@catch(sqlException e){
  36. // TODO Auto-generated catch block
  37. e.printStackTrace();
  38. }@H_404_56@finally{
  39. rs =@H_404_56@null;
  40. }
  41. }
  42. @H_404_56@if(st!=@H_404_56@null){
  43. st.close();
  44. st=@H_404_56@null;
  45. @H_404_56@if(conn !=@H_404_56@null){
  46. conn.close();
  47. conn =@H_404_56@null;
  48. }
  49. }
四 在应用程序中如何访问存储过程
1、构建测试类: TestProcedure
  1. @H_404_56@package demo.oracle;
  2. //import static org.junit.Assert.*;
  3. @H_404_56@import java.sql.CallableStatement;
  4. @H_404_56@import java.sql.Connection;
  5. @H_404_56@import oracle.jdbc.OracleTypes;
  6. @H_404_56@import org.junit.Test;
  7. @H_404_56@import demo.utils.JDBCUtils;
  8. @H_404_56@public@H_404_56@classTestProcedure{
  9. /*
  10. create or replace procedure queryempinformation(eno in number,
  11. pename out varchar2,
  12. psal out number,
  13. pjob out varchar2)
  14. * */
  15. @Test
  16. @H_404_56@public@H_404_56@void testProdedure(){
  17. String sql ="{call queryempinformation(?,?,?)}";
  18. Connection conn =@H_404_56@null;
  19. CallableStatement call=@H_404_56@null;
  20. @H_404_56@try{
  21. //得到一个连接
  22. conn =JDBCUtils.getConnection();
  23. call = conn.prepareCall(sql);
  24. //对于输入参数,赋值
  25. call.setInt(1,7839);
  26. //对于输出参数,申明
  27. call.registerOutParameter(2,OracleTypes.VARCHAR);
  28. call.registerOutParameter(3,OracleTypes.NUMBER);
  29. call.registerOutParameter(4,OracleTypes.VARCHAR);
  30. //执行调用
  31. call.execute();
  32. //取出结果
  33. String name = call.getString(2);
  34. @H_404_56@double sal =call.getDouble(3);
  35. String job = call.getString(4);
  36. System.out.println(name+"\t"+sal+"\t"+job);
  37. }@H_404_56@catch(Exception e){
  38. e.printStackTrace();
  39. }@H_404_56@finally{
  40. JDBCUtils.release(conn, call,@H_404_56@null);
  41. }
  42. }
  43. }
2、测试结果:
KING10100.0PRESIDENT
五 在应用程序中如何访问存储函数
1、构建测试类TestFunction
  1. @H_404_56@import java.sql.CallableStatement;
  2. @H_404_56@import java.sql.Connection;
  3. @H_404_56@import oracle.jdbc.OracleTypes;
  4. @H_404_56@import org.junit.Test;
  5. @H_404_56@import demo.utils.JDBCUtils;
  6. @H_404_56@public@H_404_56@classTestFunction{
  7. /*
  8. create or replace function queryempincoming(eno in number)
  9. return number
  10. * */
  11. @Test
  12. @H_404_56@public@H_404_56@void testFunction(){
  13. String sql="{call queryempincoming(?)}";
  14. Connection conn =@H_404_56@null;
  15. CallableStatement call =@H_404_56@null;
  16. @H_404_56@try{
  17. //得到数据库连接
  18. conn =JDBCUtils.getConnection();
  19. call = conn.prepareCall(sql);
  20. //对于输出函数,申明
  21. call.registerOutParameter(1,OracleTypes.NUMBER);
  22. //对于输入参数,赋值
  23. call.setInt(2,7839);
  24. //执行调用
  25. call.execute();
  26. //取出年收入结构
  27. @H_404_56@double income = call.getDouble(1);
  28. System.out.println("该员工的年收入是:"+income);
  29. }@H_404_56@catch(Exception e){
  30. e.printStackTrace();
  31. }@H_404_56@finally{
  32. 404_56@null);
  33. }
  34. }
2、测试结果
KING10100.0PRESIDENT
原文链接:https://www.f2er.com/oracle/208071.html

猜你在找的Oracle相关文章