- 浏览: 15603 次
- 性别:
- 来自: 合肥
文章分类
最新评论
oracle存储过程
http://owftc.iteye.com/blog/90032
Java代码
create or replace procedure GetRecords(name_out out varchar2,age_in in varchar2) as
begin
select NAME into name_out from test where AGE = age_in;
end;
create or replace procedure insertRecord(UserID in varchar2, UserName in varchar2,UserAge in varchar2) is
begin
insert into test values (UserID, UserName, UserAge);
end;
create or replace procedure GetRecords(name_out out varchar2,age_in in varchar2) as
begin
select NAME into name_out from test where AGE = age_in;
end;
create or replace procedure insertRecord(UserID in varchar2, UserName in varchar2,UserAge in varchar2) is
begin
insert into test values (UserID, UserName, UserAge);
end;
Java代码
-
-
首先,在Oracle中创建了一个名为TEST_SEQ的Sequence对象,SQL语句如下:
Java代码
create sequence TEST_SEQ
minvalue 100
maxvalue 999
start with 102
increment by 1
nocache;
create sequence TEST_SEQ
minvalue 100
maxvalue 999
start with 102
increment by 1
nocache;
语法应该是比较易懂的,最小最大值分别用minvalue,maxvalue表示,初始值是102(这个数字是动态变化的,我创建的时候设的是100,后因插入了2条数据后就自动增加了2),increment当然就是步长了。在PL/SQL中可以用test_seq.nextval访问下一个序列号,用test_seq.currval访问当前的序列号。
定义完了Sequence,接下来就是创建一个存储过程InsertRecordWithSequence:
--这次我修改了test表的定义,和前面的示例不同。其中,UserID是PK。
Java代码
create or replace procedure InsertRecordWithSequence(UserID out number,UserName in varchar2,UserAge in number)
is
begin insert into test(id, name, age) --插入一条记录,PK值从Sequece获取
values(test_seq.nextval, UserName, UserAge);
/*返回PK值。注意Dual表的用法*/
select test_seq.currval into UserID from dual;
end InsertRecordWithSequence;
create or replace procedure InsertRecordWithSequence(UserID out number,UserName in varchar2,UserAge in number)
is
begin insert into test(id, name, age) --插入一条记录,PK值从Sequece获取
values(test_seq.nextval, UserName, UserAge);
/*返回PK值。注意Dual表的用法*/
select test_seq.currval into UserID from dual;
end InsertRecordWithSequence;
为了让存储过程返回结果集,必须定义一个游标变量作为输出参数。这和Sql Server中有着很大的不同!并且还要用到Oracle中“包”(Package)的概念,似乎有点繁琐,但熟悉后也会觉得很方便。
关于“包”的概念,有很多内容可以参考,在此就不赘述了。首先,我创建了一个名为TestPackage的包,包头是这么定义的:
Java代码
create or replace package TestPackage is
type mycursor is ref cursor; -- 定义游标变量
procedure GetRecords(ret_cursor out mycursor); -- 定义过程,用游标变量作为返回参数
end TestPackage;
包体是这么定义的:
create or replace package body TestPackage is
/*过程体*/
procedure GetRecords(ret_cursor out mycursor) as
begin
open ret_cursor for select * from test;
end GetRecords;
end TestPackage;
create or replace package TestPackage is
type mycursor is ref cursor; -- 定义游标变量
procedure GetRecords(ret_cursor out mycursor); -- 定义过程,用游标变量作为返回参数
end TestPackage;
包体是这么定义的:
create or replace package body TestPackage is
/*过程体*/
procedure GetRecords(ret_cursor out mycursor) as
begin
open ret_cursor for select * from test;
end GetRecords;
end TestPackage;
小结:
包是Oracle特有的概念,Sql Server中找不到相匹配的东西。在我看来,包有点像VC++的类,包头就是.h文件,包体就是.cpp文件。包头只负责定义,包体则负责具体实现。如果包返回多个游标,则DataReader会按照您向参数集合中添加它们的顺序来访问这些游标,而不是按照它们在过程中出现的顺序来访问。可使用DataReader的NextResult()方法前进到下一个游标。
Java代码
-
-
Java代码
create or replace package TestPackage is
type mycursor is ref cursor;
procedure UpdateRecords(id_in in number,newName in varchar2,newAge in number);
procedure SelectRecords(ret_cursor out mycursor);
procedure DeleteRecords(id_in in number);
procedure InsertRecords(name_in in varchar2, age_in in number);
end TestPackage;
create or replace package TestPackage is
type mycursor is ref cursor;
procedure UpdateRecords(id_in in number,newName in varchar2,newAge in number);
procedure SelectRecords(ret_cursor out mycursor);
procedure DeleteRecords(id_in in number);
procedure InsertRecords(name_in in varchar2, age_in in number);
end TestPackage;
包体如下:
Java代码
create or replace package body TestPackage is
procedure UpdateRecords(id_in in number, newName in varchar2, newAge in number) as
begin
update test set age = newAge, name = newName where id = id_in;
end UpdateRecords;
procedure SelectRecords(ret_cursor out mycursor) as
begin
open ret_cursor for select * from test;
end SelectRecords;
procedure DeleteRecords(id_in in number) as
begin
delete from test where id = id_in;
end DeleteRecords;
procedure InsertRecords(name_in in varchar2, age_in in number) as
begin
insert into test values (test_seq.nextval, name_in, age_in);
--test_seq是一个已建的Sequence对象,请参照前面的示例
end InsertRecords;
end TestPackage;
create or replace package body TestPackage is
procedure UpdateRecords(id_in in number, newName in varchar2, newAge in number) as
begin
update test set age = newAge, name = newName where id = id_in;
end UpdateRecords;
procedure SelectRecords(ret_cursor out mycursor) as
begin
open ret_cursor for select * from test;
end SelectRecords;
procedure DeleteRecords(id_in in number) as
begin
delete from test where id = id_in;
end DeleteRecords;
procedure InsertRecords(name_in in varchar2, age_in in number) as
begin
insert into test values (test_seq.nextval, name_in, age_in);
--test_seq是一个已建的Sequence对象,请参照前面的示例
end InsertRecords;
end TestPackage;
--------------------华丽的分割线-----------------------
http://oraclex.iteye.com/blog/814442
实现存储过程必须先在oracle建立相应的Procedures,如下所示:
Sql代码
--添加信息--
create or replace procedure insert_t_test(
p_id in number,
p_name in varchar2,
p_password in varchar2
) is
begin
insert into t_test(id,name,password) values(p_id,p_name,p_password);
end;
-------------------------
--删除信息--
create or replace procedure del_t_test(
p_id in number,
x_out_record out number) is
begin
delete t_test where id = p_id;
x_out_record := 0;
exception
when others then
x_out_record := -1;
end;
-----------------------------
--查询所有信息--
create or replace procedure all_t_test(
x_out_record out number,
x_out_cursor out sys_refcursor) is
begin
open x_out_cursor for
select * from t_test;
x_out_record := 0;
exception
when others then
x_out_record := -1;
end;
--添加信息--
create or replace procedure insert_t_test(
p_id in number,
p_name in varchar2,
p_password in varchar2
) is
begin
insert into t_test(id,name,password) values(p_id,p_name,p_password);
end;
-------------------------
--删除信息--
create or replace procedure del_t_test(
p_id in number,
x_out_record out number) is
begin
delete t_test where id = p_id;
x_out_record := 0;
exception
when others then
x_out_record := -1;
end;
-----------------------------
--查询所有信息--
create or replace procedure all_t_test(
x_out_record out number,
x_out_cursor out sys_refcursor) is
begin
open x_out_cursor for
select * from t_test;
x_out_record := 0;
exception
when others then
x_out_record := -1;
end;
其中的存储过程名字(就是加粗部分)必须要和java代码中的相对应
Java代码如下:
Java代码
package com.procedure.db;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import oracle.jdbc.OracleTypes;
public class ConnDB {
private String url="jdbc:oracle:thin:@localhost:1521:orcl";
private String driverClass="oracle.jdbc.driver.OracleDriver";
private String username="scott";
private String password="hello";
public Connection getConn(){
Connection conn=null;
try {
Class.forName(driverClass);
conn=DriverManager.getConnection(url,username,password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void main(String args[]){
ConnDB cd=new ConnDB();
Connection conn=cd.getConn();
/**
* 添加 有输入参数 无输出参数
*/
/*try {
CallableStatement call=conn.prepareCall("{call insert_t_test(?,?,?)}");
call.setInt(1, 66);
call.setString(2, "小猫");
call.setString(3, "8989");
Boolean b=call.execute();
System.out.println("b="+b);
} catch (SQLException e) {
e.printStackTrace();
}*/
/**
* 删除 有输入参数 得到输出参数
*/
/*try {
CallableStatement call=conn.prepareCall("{call del_t_test(?,?)}");
call.setInt(1, 66);
call.registerOutParameter(2, Types.INTEGER);
call.execute();
Integer result=call.getInt(2);
System.out.println("执行结果为0正常,执行结果为-1不正常"+result);
} catch (SQLException e) {
e.printStackTrace();
}*/
/**
* 使用游标查询所有信息 无输入参数 有输出参数
*/
try {
CallableStatement call=conn.prepareCall("{call all_t_test(?,?)}");
call.registerOutParameter(1, Types.INTEGER);
call.registerOutParameter(2, OracleTypes.CURSOR);
call.execute();
Integer result=call.getInt(1);
ResultSet rs=(ResultSet) call.getObject(2);
while(rs.next()){
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package com.procedure.db;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import oracle.jdbc.OracleTypes;
public class ConnDB {
private String url="jdbc:oracle:thin:@localhost:1521:orcl";
private String driverClass="oracle.jdbc.driver.OracleDriver";
private String username="scott";
private String password="hello";
public Connection getConn(){
Connection conn=null;
try {
Class.forName(driverClass);
conn=DriverManager.getConnection(url,username,password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void main(String args[]){
ConnDB cd=new ConnDB();
Connection conn=cd.getConn();
/**
* 添加 有输入参数 无输出参数
*/
/*try {
CallableStatement call=conn.prepareCall("{call insert_t_test(?,?,?)}");
call.setInt(1, 66);
call.setString(2, "小猫");
call.setString(3, "8989");
Boolean b=call.execute();
System.out.println("b="+b);
} catch (SQLException e) {
e.printStackTrace();
}*/
/**
* 删除 有输入参数 得到输出参数
*/
/*try {
CallableStatement call=conn.prepareCall("{call del_t_test(?,?)}");
call.setInt(1, 66);
call.registerOutParameter(2, Types.INTEGER);
call.execute();
Integer result=call.getInt(2);
System.out.println("执行结果为0正常,执行结果为-1不正常"+result);
} catch (SQLException e) {
e.printStackTrace();
}*/
/**
* 使用游标查询所有信息 无输入参数 有输出参数
*/
try {
CallableStatement call=conn.prepareCall("{call all_t_test(?,?)}");
call.registerOutParameter(1, Types.INTEGER);
call.registerOutParameter(2, OracleTypes.CURSOR);
call.execute();
Integer result=call.getInt(1);
ResultSet rs=(ResultSet) call.getObject(2);
while(rs.next()){
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
-----------------------------------华丽的分割线------------------------
http://www.cnblogs.com/liliu/archive/2011/06/22/2087546.html
Oracle 存储过程 简要记录存储过程语法与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 isbegin 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) isbegin 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);
上述存储过程修改完毕。另外,一个复杂的工程项目中的例子:查询一段数据中间隔不超过十分钟且连续超过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;
/
http://owftc.iteye.com/blog/90032
Java代码
create or replace procedure GetRecords(name_out out varchar2,age_in in varchar2) as
begin
select NAME into name_out from test where AGE = age_in;
end;
create or replace procedure insertRecord(UserID in varchar2, UserName in varchar2,UserAge in varchar2) is
begin
insert into test values (UserID, UserName, UserAge);
end;
create or replace procedure GetRecords(name_out out varchar2,age_in in varchar2) as
begin
select NAME into name_out from test where AGE = age_in;
end;
create or replace procedure insertRecord(UserID in varchar2, UserName in varchar2,UserAge in varchar2) is
begin
insert into test values (UserID, UserName, UserAge);
end;
Java代码
-
-
首先,在Oracle中创建了一个名为TEST_SEQ的Sequence对象,SQL语句如下:
Java代码
create sequence TEST_SEQ
minvalue 100
maxvalue 999
start with 102
increment by 1
nocache;
create sequence TEST_SEQ
minvalue 100
maxvalue 999
start with 102
increment by 1
nocache;
语法应该是比较易懂的,最小最大值分别用minvalue,maxvalue表示,初始值是102(这个数字是动态变化的,我创建的时候设的是100,后因插入了2条数据后就自动增加了2),increment当然就是步长了。在PL/SQL中可以用test_seq.nextval访问下一个序列号,用test_seq.currval访问当前的序列号。
定义完了Sequence,接下来就是创建一个存储过程InsertRecordWithSequence:
--这次我修改了test表的定义,和前面的示例不同。其中,UserID是PK。
Java代码
create or replace procedure InsertRecordWithSequence(UserID out number,UserName in varchar2,UserAge in number)
is
begin insert into test(id, name, age) --插入一条记录,PK值从Sequece获取
values(test_seq.nextval, UserName, UserAge);
/*返回PK值。注意Dual表的用法*/
select test_seq.currval into UserID from dual;
end InsertRecordWithSequence;
create or replace procedure InsertRecordWithSequence(UserID out number,UserName in varchar2,UserAge in number)
is
begin insert into test(id, name, age) --插入一条记录,PK值从Sequece获取
values(test_seq.nextval, UserName, UserAge);
/*返回PK值。注意Dual表的用法*/
select test_seq.currval into UserID from dual;
end InsertRecordWithSequence;
为了让存储过程返回结果集,必须定义一个游标变量作为输出参数。这和Sql Server中有着很大的不同!并且还要用到Oracle中“包”(Package)的概念,似乎有点繁琐,但熟悉后也会觉得很方便。
关于“包”的概念,有很多内容可以参考,在此就不赘述了。首先,我创建了一个名为TestPackage的包,包头是这么定义的:
Java代码
create or replace package TestPackage is
type mycursor is ref cursor; -- 定义游标变量
procedure GetRecords(ret_cursor out mycursor); -- 定义过程,用游标变量作为返回参数
end TestPackage;
包体是这么定义的:
create or replace package body TestPackage is
/*过程体*/
procedure GetRecords(ret_cursor out mycursor) as
begin
open ret_cursor for select * from test;
end GetRecords;
end TestPackage;
create or replace package TestPackage is
type mycursor is ref cursor; -- 定义游标变量
procedure GetRecords(ret_cursor out mycursor); -- 定义过程,用游标变量作为返回参数
end TestPackage;
包体是这么定义的:
create or replace package body TestPackage is
/*过程体*/
procedure GetRecords(ret_cursor out mycursor) as
begin
open ret_cursor for select * from test;
end GetRecords;
end TestPackage;
小结:
包是Oracle特有的概念,Sql Server中找不到相匹配的东西。在我看来,包有点像VC++的类,包头就是.h文件,包体就是.cpp文件。包头只负责定义,包体则负责具体实现。如果包返回多个游标,则DataReader会按照您向参数集合中添加它们的顺序来访问这些游标,而不是按照它们在过程中出现的顺序来访问。可使用DataReader的NextResult()方法前进到下一个游标。
Java代码
-
-
Java代码
create or replace package TestPackage is
type mycursor is ref cursor;
procedure UpdateRecords(id_in in number,newName in varchar2,newAge in number);
procedure SelectRecords(ret_cursor out mycursor);
procedure DeleteRecords(id_in in number);
procedure InsertRecords(name_in in varchar2, age_in in number);
end TestPackage;
create or replace package TestPackage is
type mycursor is ref cursor;
procedure UpdateRecords(id_in in number,newName in varchar2,newAge in number);
procedure SelectRecords(ret_cursor out mycursor);
procedure DeleteRecords(id_in in number);
procedure InsertRecords(name_in in varchar2, age_in in number);
end TestPackage;
包体如下:
Java代码
create or replace package body TestPackage is
procedure UpdateRecords(id_in in number, newName in varchar2, newAge in number) as
begin
update test set age = newAge, name = newName where id = id_in;
end UpdateRecords;
procedure SelectRecords(ret_cursor out mycursor) as
begin
open ret_cursor for select * from test;
end SelectRecords;
procedure DeleteRecords(id_in in number) as
begin
delete from test where id = id_in;
end DeleteRecords;
procedure InsertRecords(name_in in varchar2, age_in in number) as
begin
insert into test values (test_seq.nextval, name_in, age_in);
--test_seq是一个已建的Sequence对象,请参照前面的示例
end InsertRecords;
end TestPackage;
create or replace package body TestPackage is
procedure UpdateRecords(id_in in number, newName in varchar2, newAge in number) as
begin
update test set age = newAge, name = newName where id = id_in;
end UpdateRecords;
procedure SelectRecords(ret_cursor out mycursor) as
begin
open ret_cursor for select * from test;
end SelectRecords;
procedure DeleteRecords(id_in in number) as
begin
delete from test where id = id_in;
end DeleteRecords;
procedure InsertRecords(name_in in varchar2, age_in in number) as
begin
insert into test values (test_seq.nextval, name_in, age_in);
--test_seq是一个已建的Sequence对象,请参照前面的示例
end InsertRecords;
end TestPackage;
--------------------华丽的分割线-----------------------
http://oraclex.iteye.com/blog/814442
实现存储过程必须先在oracle建立相应的Procedures,如下所示:
Sql代码
--添加信息--
create or replace procedure insert_t_test(
p_id in number,
p_name in varchar2,
p_password in varchar2
) is
begin
insert into t_test(id,name,password) values(p_id,p_name,p_password);
end;
-------------------------
--删除信息--
create or replace procedure del_t_test(
p_id in number,
x_out_record out number) is
begin
delete t_test where id = p_id;
x_out_record := 0;
exception
when others then
x_out_record := -1;
end;
-----------------------------
--查询所有信息--
create or replace procedure all_t_test(
x_out_record out number,
x_out_cursor out sys_refcursor) is
begin
open x_out_cursor for
select * from t_test;
x_out_record := 0;
exception
when others then
x_out_record := -1;
end;
--添加信息--
create or replace procedure insert_t_test(
p_id in number,
p_name in varchar2,
p_password in varchar2
) is
begin
insert into t_test(id,name,password) values(p_id,p_name,p_password);
end;
-------------------------
--删除信息--
create or replace procedure del_t_test(
p_id in number,
x_out_record out number) is
begin
delete t_test where id = p_id;
x_out_record := 0;
exception
when others then
x_out_record := -1;
end;
-----------------------------
--查询所有信息--
create or replace procedure all_t_test(
x_out_record out number,
x_out_cursor out sys_refcursor) is
begin
open x_out_cursor for
select * from t_test;
x_out_record := 0;
exception
when others then
x_out_record := -1;
end;
其中的存储过程名字(就是加粗部分)必须要和java代码中的相对应
Java代码如下:
Java代码
package com.procedure.db;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import oracle.jdbc.OracleTypes;
public class ConnDB {
private String url="jdbc:oracle:thin:@localhost:1521:orcl";
private String driverClass="oracle.jdbc.driver.OracleDriver";
private String username="scott";
private String password="hello";
public Connection getConn(){
Connection conn=null;
try {
Class.forName(driverClass);
conn=DriverManager.getConnection(url,username,password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void main(String args[]){
ConnDB cd=new ConnDB();
Connection conn=cd.getConn();
/**
* 添加 有输入参数 无输出参数
*/
/*try {
CallableStatement call=conn.prepareCall("{call insert_t_test(?,?,?)}");
call.setInt(1, 66);
call.setString(2, "小猫");
call.setString(3, "8989");
Boolean b=call.execute();
System.out.println("b="+b);
} catch (SQLException e) {
e.printStackTrace();
}*/
/**
* 删除 有输入参数 得到输出参数
*/
/*try {
CallableStatement call=conn.prepareCall("{call del_t_test(?,?)}");
call.setInt(1, 66);
call.registerOutParameter(2, Types.INTEGER);
call.execute();
Integer result=call.getInt(2);
System.out.println("执行结果为0正常,执行结果为-1不正常"+result);
} catch (SQLException e) {
e.printStackTrace();
}*/
/**
* 使用游标查询所有信息 无输入参数 有输出参数
*/
try {
CallableStatement call=conn.prepareCall("{call all_t_test(?,?)}");
call.registerOutParameter(1, Types.INTEGER);
call.registerOutParameter(2, OracleTypes.CURSOR);
call.execute();
Integer result=call.getInt(1);
ResultSet rs=(ResultSet) call.getObject(2);
while(rs.next()){
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package com.procedure.db;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import oracle.jdbc.OracleTypes;
public class ConnDB {
private String url="jdbc:oracle:thin:@localhost:1521:orcl";
private String driverClass="oracle.jdbc.driver.OracleDriver";
private String username="scott";
private String password="hello";
public Connection getConn(){
Connection conn=null;
try {
Class.forName(driverClass);
conn=DriverManager.getConnection(url,username,password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public static void main(String args[]){
ConnDB cd=new ConnDB();
Connection conn=cd.getConn();
/**
* 添加 有输入参数 无输出参数
*/
/*try {
CallableStatement call=conn.prepareCall("{call insert_t_test(?,?,?)}");
call.setInt(1, 66);
call.setString(2, "小猫");
call.setString(3, "8989");
Boolean b=call.execute();
System.out.println("b="+b);
} catch (SQLException e) {
e.printStackTrace();
}*/
/**
* 删除 有输入参数 得到输出参数
*/
/*try {
CallableStatement call=conn.prepareCall("{call del_t_test(?,?)}");
call.setInt(1, 66);
call.registerOutParameter(2, Types.INTEGER);
call.execute();
Integer result=call.getInt(2);
System.out.println("执行结果为0正常,执行结果为-1不正常"+result);
} catch (SQLException e) {
e.printStackTrace();
}*/
/**
* 使用游标查询所有信息 无输入参数 有输出参数
*/
try {
CallableStatement call=conn.prepareCall("{call all_t_test(?,?)}");
call.registerOutParameter(1, Types.INTEGER);
call.registerOutParameter(2, OracleTypes.CURSOR);
call.execute();
Integer result=call.getInt(1);
ResultSet rs=(ResultSet) call.getObject(2);
while(rs.next()){
System.out.println(rs.getInt(1));
System.out.println(rs.getString(2));
System.out.println(rs.getString(3));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
-----------------------------------华丽的分割线------------------------
http://www.cnblogs.com/liliu/archive/2011/06/22/2087546.html
Oracle 存储过程 简要记录存储过程语法与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 isbegin 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) isbegin 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);
上述存储过程修改完毕。另外,一个复杂的工程项目中的例子:查询一段数据中间隔不超过十分钟且连续超过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;
/
相关推荐
本文将从 Oracle 存储过程的基础知识开始,逐步深入到 Oracle 存储过程的高级应用,包括 Hibernate 调用 Oracle 存储过程和 Java 调用 Oracle 存储过程的方法。 Oracle 存储过程基础知识 Oracle 存储过程是 Oracle...
Oracle存储过程unwrap解密工具主要用于处理Oracle数据库中的加密存储过程。在Oracle数据库系统中,为了保护敏感代码或数据,开发人员有时会选择对存储过程进行加密。然而,当需要查看、调试或恢复这些加密的存储过程...
总结起来,"帆软报表Oracle存储过程解决storeParameter1参数试用插件"主要是针对在调用无参数Oracle存储过程时出现的异常问题提供的一种解决方案。通过安装并配置这个插件,用户可以顺利地在帆软报表中调用不包含...
以下是对“oracle存储过程解锁”这一主题的深入解析。 ### 标题:“oracle存储过程解锁” #### 解析: 在Oracle数据库中,存储过程是一种预先编译并存储在数据库中的SQL代码块,用于执行复杂的业务逻辑或数据处理...
本文实例讲述了Python使用cx_Oracle调用Oracle存储过程的方法。分享给大家供大家参考,具体如下: 这里主要测试在Python中通过cx_Oracle调用PL/SQL。 首先,在数据库端创建简单的存储过程。 create or replace ...
Oracle存储过程是数据库管理系统中的一种重要特性,它允许开发者编写一系列SQL语句和PL/SQL块,形成可重复使用的代码单元。这篇博客“oracle存储过程-帮助文档”可能提供了关于如何创建、调用和管理Oracle存储过程...
### Oracle存储过程、函数与DBLink详解 #### 一、Oracle存储过程简介 在Oracle数据库中,存储过程是一种预编译好的SQL代码集合,它可以接受输入参数、返回单个值或多个值,并能够执行复杂的数据库操作。存储过程...
以下是一个简单的示例,展示了如何调用一个不带参数的Oracle存储过程: ```java Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); session....
本文将深入探讨如何在Spring Boot项目中整合MyBatis,实现调用Oracle存储过程并处理游标返回的数据。 首先,我们需要在Spring Boot项目中引入相关的依赖。在`pom.xml`文件中添加Oracle JDBC驱动(ojdbc66-oracle...
本篇文章将深入探讨如何在Oracle存储过程中使用临时表,包括会话级临时表和事务级临时表。 ### 会话级临时表 会话级临时表(Session-Level Temporary Tables)只在创建它的会话内可见,并且在会话结束时自动删除。...
Oracle存储过程是数据库管理系统Oracle中的一个关键特性,它允许开发者编写一组预编译的SQL和PL/SQL语句,以实现特定的业务逻辑或数据库操作。这篇教程将深入讲解Oracle存储过程的各个方面,帮助你从基础到高级全面...
本文将详细讲解如何在C#中使用自定义列表(List)作为参数调用Oracle存储过程,以及实现这一功能的关键技术和注意事项。 首先,我们需要了解Oracle数据库中的PL/SQL类型,例如VARCAR2、NUMBER等,它们对应于C#中的...
### Oracle存储过程批量提交知识点详解 在Oracle数据库中,存储过程是一种重要的数据库对象,它可以包含一系列SQL语句和控制流语句,用于实现复杂的业务逻辑处理。存储过程不仅可以提高应用程序性能,还可以确保...
标题中的“pb中执行oracle存储过程脚本”指的是在PowerBuilder(简称PB)环境中调用Oracle数据库的存储过程。PowerBuilder是一种可视化的开发工具,常用于构建数据驱动的应用程序。Oracle存储过程则是在Oracle数据库...
oracle 存储过程导出excel oracle 存储过程导出excel oracle 存储过程导出excel oracle 存储过程导出excel oracle 存储过程导出excel
Oracle 存储过程调用 CallabledStatement 实用例子(IN OUT 传游标) 一、Oracle 存储过程简介 Oracle 存储过程是一种可以在 Oracle 数据库中存储和执行的程序单元。存储过程可以由多种语言编写,例如 PL/SQL、...
本篇将深入探讨如何在Oracle存储过程中创建并返回一个结果集,并结合Java代码展示如何在应用程序中使用这个结果集。 首先,我们需要理解`OUT`参数的概念。在Oracle存储过程中,`IN`参数用于传递数据到过程,`OUT`...
可以将SQL Server存储过程转为oracle存储过程的工具
Oracle存储过程常用技巧 Oracle存储过程是一种强大的数据库对象,它可以帮助开发者简化复杂的业务逻辑,并提高数据库的安全性和性能。在 Oracle 中,存储过程是一种特殊的 PL/SQL 程序,它可以接受输入参数,执行...
本话题将详细探讨如何在Oracle存储过程中调用外部的批处理脚本,如Windows系统的BAT文件,以实现数据库操作与系统命令的集成。 首先,`Oracle存储过程`是一种预编译的SQL和PL/SQL代码集合,可以被多次调用以执行...