zoukankan      html  css  js  c++  java
  • Oracle 存储过程了解

    简要记录存储过程语法与Java程序的调用方式

      一 存储过程

        首先,我们建立一个简单的表进行存储过程的测试

    createtable
    xuesheng(id
    integer, xing_ming varchar2(25), yu_wen number, shu_xue number);

    insertinto xuesheng values(1,'zhangsan',80,90)
    insertinto xuesheng values(2,'lisi',85,87)

    1)无返回值的存储过程

    createorreplaceprocedure xs_proc_no is
    begin
    insertinto xuesheng values (3, 'wangwu', 90, 90);
    commit;
    end xs_proc_no;

    2)有单个数据值返回的存储过程

    复制代码
    createorreplaceprocedure xs_proc(temp_name invarchar2,
    temp_num out
    number) is
    num_1
    number;
    num_2
    number;
    begin
    select yu_wen, shu_xue
    into num_1, num_2
    from xuesheng
    where xing_ming = temp_name;
    --dbms_output.put_line(num_1 + num_2);
    temp_num := num_1 + num_2;
    end;
    复制代码

    其中,以上两种与sql server基本类似,而对于返回数据集时,上述方法则不能满足我们的要求。在Oracle中,一般使用ref cursor来返回数据集。示例代码如下:

    3)有返回值的存储过程(列表返回)

    首先,建立我们自己的包。并定义包中的一个自定义ref cursor

    createorreplace package mypackage as
    type my_cursor
    is ref cursor;
    end mypackage;

    在定义了ref cursor后,可以书写我们的程序代码

    createorreplaceprocedure xs_proc_list(shuxue innumber,
    p_cursor out mypackage.my_cursor)
    is
    begin
    open p_cursor for
    select*from xuesheng where shu_xue > shuxue;
    end xs_proc_list;

     二、程序调用

    在本节中,我们使用java语言调用存储过程。其中,关键是使用CallableStatement这个对象,代码如下:

    String oracleDriverName = "oracle.jdbc.driver.OracleDriver";
     
            // 以下使用的Test就是Oracle里的表空间
            String oracleUrlToConnect = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
            Connection myConnection = null;
            try {
                Class.forName(oracleDriverName);
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            }
            try {
                myConnection = DriverManager.getConnection(oracleUrlToConnect,
                        "xxxx", "xxxx");//此处为数据库用户名与密码
     
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            try {
                 
                CallableStatement proc=null;
                proc=myConnection.prepareCall("{call xs_proc(?,?)}");
                proc.setString(1, "zhangsan");
                proc.registerOutParameter(2, Types.NUMERIC);
                proc.execute();
                String teststring=proc.getString(2);
                System.out.println(teststring);
     
            } catch (Exception ex) {
                ex.printStackTrace();
            }

    对于列表返回值的存储过程,在上述代码中做简单修改。如下

    复制代码
    CallableStatement proc=null;
    proc
    =myConnection.prepareCall("{call getdcsj(?,?,?,?,?)}");
    proc.setString(
    1, strDate);
    proc.setString(
    2, jzbh);
    proc.registerOutParameter(
    3, Types.NUMERIC);
    proc.registerOutParameter(
    4, OracleTypes.CURSOR);
    proc.registerOutParameter(
    5, OracleTypes.CURSOR);
    proc.execute();
    ResultSet rs
    =null;
    int total_number=proc.getInt(3);
    rs
    =(ResultSet)proc.getObject(4);
    复制代码
  • 相关阅读:
    25-javaweb接入支付宝支付接口
    4-js 函数
    24-filter-拦截器
    23-新建maven 项目
    22-maven-安装与配置
    15-matlab矩阵运用
    2018.7.18 div,section,article的区别和使用
    2018.7.17 牛客网训练
    2018.7.16常用推荐算法
    2018.7.15 解决css中input输入框点击时去掉外边框方法
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/3700667.html
Copyright © 2011-2022 走看看