设为首页 加入收藏

TOP

Oracle存储过程(二)
2014-11-24 02:08:19 来源: 作者: 【 】 浏览:1
Tags:Oracle 存储 过程
kage mypackage as
type my_cursor is ref cursor;
end mypackage;
在定义了ref cursor后,可以书写我们的程序代码

create or replace procedure xs_proc_list(shuxue in number,
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这个对象,代码如下:
view sourceprint
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);
上述存储过程修改完毕。另外,一个复杂的工程项目中的例子:查询一段数据中间隔不超过十分钟且连续超过100条的数据。即上述代码所调用的getdcsj存储过程
view sourceprint
create or replace procedure getDcsj(var_flag in varchar2,
var_jzbh in varchar2,
number_total out number,
var_cursor_a out mypackage.my_cursor,
var_cursor_b out mypackage.my_cursor) is
total number;
cursor cur is
select sj, flag
from d_dcsj
where jzbh = var_jzbh
order by sj desc
for update;
last_time date;
begin
for cur1 in cur loop
if last_time is null or cur1.sj >= last_time - 10 / 60 / 24 then
update d_dcsj set flag = var_flag where current of cur;
last_time := cur1.sj;
else
select count(*) into total from d_dcsj where flag = var_flag;
dbms_output.put_line(total);
if total < 100 then
update d_dcsj set flag = null where flag = var_flag;
last_time := null;
update d_dcsj set flag = var_flag where current of cur;
else
open var_cursor_a for
select *
from d_dcsj
where flag = var_flag
and jzbh = var_jzbh
and zh = 'A'
order by sj desc;
number_total := total;
open var_cursor_b for
select *
from d_dcsj
where flag =

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇ORACLE集合简单应用实例 下一篇oracle Merge的使用小结

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: