一:无返回值的存储过程
表:
create table person(id number not null primary key,name varchar2(20),age int);
存储过程为:
CREATE OR REPLACE PROCEDURE insertperson(name IN VARCHAR2,age IN int) AS
BEGIN
INSERT INTO person VALUES (seq_id.nextval,name, age);
commit;
END insertperson;
Java代码:
public static Connection getConnection(){
Connection con = null ;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orclzy", "zy", "oracle");
} catch (ClassNotFoundException e) {
System.out.println("驱动找不到");
e.printStackTrace();
} catch (SQLException e) {
System.out.println("连接数据库出现异常");
e.printStackTrace();
}
return con ;
}
public void insertperson(String name,int age){
Connection con = ProcedureTest.getConnection() ;
CallableStatement cst = null ;
try {
cst = con.prepareCall("{call insertperson(?,?)}");
cst.setString(1, name);
cst.setInt(2, age);
cst.execute();
System.out.println("插入数据成功");
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
cst.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
二:有返回值的存储过程(单个返回值)
存储过程为:
CREATE OR REPLACE PROCEDURE selectNameByid(pid IN number,pname OUT VARCHAR2) AS
BEGIN
SELECT name INTO pname FROM person WHERE id=pid;
END selectNameByid;
Java代码:
public void findNamepersonByid(int id){
Connection con = ProcedureTest.getConnection() ;
ResultSet rs = null ;
CallableStatement cst = null ;
try {
cst = con.prepareCall("{call selectNameByid(?,?)}");
cst.setInt(1, id);
cst.registerOutParameter(2, Types.VARCHAR);
cst.execute();
String name = cst.getString(2);
System.out.println("name:"+name);
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
cst.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
三:返回列表
由于oracle存储过程没有返回值,它的所有返回值都是通过out参数来替代的,
列表同样也不例外,但由于是集合,所以不能用一般的参数,必须要用pagkage了.可分两部分实现:
1、建一个程序包。如下:
CREATE OR REPLACE PACKAGE PERSONPACKAGE AS
TYPE PERSON_CURSOR IS REF CURSOR;
end PERSONPACKAGE;
/
2、建立存储过程,存储过程为:
CREATE OR REPLACE PROCEDURE findallperson(p_CURSOR out PERSONPACKAGE.PERSON_CURSOR) IS
BEGIN
OPEN p_CURSOR FOR SELECT * FROM person;
END findallperson;
可以看到,它是把游标(可以理解为一个指针),作为一个out 参数来返回值的。
在java里调用时就用下面的代码:
public void findAllPerson(){
Connection con = ProcedureTest.getConnection() ;
ResultSet rs = null ;
CallableStatement cst = null ;
try {
cst = con.prepareCall("{call findallperson(?)}");
cst.registerOutParameter(1, OracleTypes.CURSOR);
cst.execute();
rs = (ResultSet)cst.getObject(1);
while(rs.next()){
System.out.println(rs.getInt("id")+" "+rs.getString("name")+" "+rs.getInt("age"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
rs.close();
cst.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
在这里要注意,在执行前一定要先把oracle的驱动包放到class路径里,否则会报错的。
-------------------------------------------------------------------------------
四、jdbc调用自定义函数
CREATE OR REPLACE FUNCTION get_age(pid in person.id%type)
return number
is
v_age person.id%type :=0;
begin
select age into v_age from person where id=pid;
return v_age;
end;
/
在java代码里去调用函数:
public void invokeFunction() {
Connection con = null ;
CallableStatement cs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Properties p = new Properties();
p.load(new FileInputStream("config.properties"));
con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orclzy", p);
System.out.println("con: " + con);
// 调用一个没有参数的函数 函数返回 a int 预处理callable语句
cs = con.prepareCall("{? = call get_age(1)}");
// 注册返回值类型
cs.registerOutParameter(1, Types.INTEGER);
// Execute and retrieve the returned value
cs.execute();
int age = cs.getInt(1);
System.out.println("age:"+age);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
con.close();
cs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}