儲存過程語法與Java程式的呼叫方式

lygle發表於2013-05-23

簡要記錄儲存過程語法與Java程式的呼叫方式

一 儲存過程

    首先,我們建立一個簡單的表進行儲存過程的測試

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

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

1)無返回值的儲存過程

create or replace procedure xs_proc_no is
begin
insert into xuesheng values (3, 'wangwu', 90, 90);
commit;
end xs_proc_no;

2)有單個資料值返回的儲存過程

create or replace procedure xs_proc(temp_name in varchar2,
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

create or replace package 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這個物件,程式碼如下:

String racleDriverName = "oracle.jdbc.driver.OracleDriver";
// 以下使用的Test就是Oracle裡的表空間
String racleUrlToConnect = "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儲存過程

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 = var_flag
and jzbh = var_jzbh
and zh = 'B'
order by sj desc;
number_total := total;
exit;
end if;
end if;
end loop;
select count(*) into total from d_dcsj where flag = var_flag;
dbms_output.put_line(total);
if total < 100 then
open var_cursor_a for
select * from d_dcsj where zh = 'C';
open var_cursor_b for
select * from d_dcsj where zh = 'C';
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 = var_flag
and jzbh = var_jzbh
and zh = 'B'
order by sj desc;
number_total := total;
end if;
commit;
end;
/

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28673746/viewspace-761861/,如需轉載,請註明出處,否則將追究法律責任。

相關文章