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);
复制代码
复制代码

posted on   荣锋亮  阅读(249)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示