oracle存储过程demo1---无返回值的存储过程:
/*
写一个过程,可以向book表添加书
*/
create table book(
id number(4),
book_name varchar2(30),
publishing varchar2(30)
)
create or replace procedure add_book
(
id in number,
name in varchar2,
publishing in varchar2
)is
begin
insert into book values(id,name,publishing);
commit;
end;
package com.etc.test;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
public class CunChuDemo1 {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");//加载驱动
Connection ct = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "orcl");//获取链接
CallableStatement cs = ct.prepareCall("call add_book(?,?,?)");//创建CallableStatement
//给?赋值
cs.setInt(1, 1);
cs.setString(2, "java");
cs.setString(3, "java出版社");
//执行
cs.execute();
//关闭
cs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
==============================================================
oracle存储过程demo2---有返回值的存储过程(非列表):
/*
编写一个存储过程,可以输入雇员的编号,返回该雇员的姓名。
*/
create or replace procedure sp_proc
(
spno in number,
spname out varchar2
)is
begin
select ename into spname from emp where empno=spno;
end;
package com.etc.test;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
public class CunChuDemo1 {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver"); // 1.加载驱动
Connection ct = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "orcl");// 2.得到连接
CallableStatement cs = ct.prepareCall("call sp_proc(?,?)");// 3.创建CallableStatement
cs.setInt(1, 7788); //给第一个?赋值
cs.registerOutParameter(2, oracle.jdbc.OracleTypes.VARCHAR);//给第二个?赋值
cs.execute();//4、执行
//取出返回值,要注意?的顺序
String name = cs.getString(2);
System.out.println("编号7788的名字:" + name);
cs.close();//5、关闭
} catch (Exception e) {
e.printStackTrace();
}
}
}
----------------------------------------------------------------
扩张:编写一个过程,可以输入雇员的编号,返回该雇员的姓名、工资和岗位。
create or replace procedure sp_proc
(
spno in number,
spname out varchar2,
spsal out number,
spjob out varchar2
)is
begin
select ename,sal,job into spname,spsal,spjob from emp where empno=spno;
end;
package com.etc.test;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
public class CunChuDemo1 {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");// 1.加载驱动
Connection ct = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "orcl");// 2.得到连接
CallableStatement cs = ct.prepareCall("call sp_proc(?,?,?,?)"); // 3.创建CallableStatement
cs.setInt(1, 7788); //给第一个?赋值
cs.registerOutParameter(2, oracle.jdbc.OracleTypes.VARCHAR); //给第2个?赋值
cs.registerOutParameter(3, oracle.jdbc.OracleTypes.DOUBLE); //给第3个?赋值
cs.registerOutParameter(4, oracle.jdbc.OracleTypes.VARCHAR); //给第4个?赋值
cs.execute(); //4、执行
//取出返回值,要注意?的顺序
String name = cs.getString(2);
double sal = cs.getDouble(3);
String job = cs.getString(4);
System.out.println("编号7788的名字:" + name + ",职位" + job + ",薪水" + sal + "");
cs.close(); //5、关闭
} catch (Exception e) {
e.printStackTrace();
}
}
}
====================================================================
oracle存储过程demo3---有返回值的存储过程(列表[结果集]):
/*
编写一个存储过程,输入部门号,返回该部门所有雇员信息。
*/
分析如下:由于oracle存储过程没有返回值,它的所有返回值都是通过out参数来替代的,列表同样也不例外,
但由于是集合,所以不能用一般的参数,必须要用pagkage了。所以要分两部分:
1)、建立一个包,在该包中我们定义类型test_cursor,它是个游标。
create or replace package testpackage
as
type test_cursor is ref cursor;
end testpackage;
2)、建立存储过程。
create or replace procedure sp_proc
(
spno in number,
p_cursor out testpackage.test_cursor
)is
begin
open p_cursor for
select * from emp where deptno=spno;
end sp_proc;
package com.etc.test;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class CunChuDemo1 {
public static void main(String[] args) {
try {
Class.forName("oracle.jdbc.driver.OracleDriver"); // 1.加载驱动
// 2.得到连接
Connection ct = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "orcl");
CallableStatement cs = ct.prepareCall("call sp_proc(?,?)"); // 3.创建CallableStatement
cs.setInt(1, 10); //给第1个?赋值
cs.registerOutParameter(2, oracle.jdbc.OracleTypes.CURSOR); //给第二个?赋值
cs.execute(); //4、执行
//得到结果集
ResultSet rs = (ResultSet) cs.getObject(2);
while (rs.next()) {
System.out.println(rs.getInt(1) + " " + rs.getString(2) + " " + rs.getString(3) + " " + rs.getInt(4)
+ " " + rs.getDate(5)+ " " + rs.getInt(6)+ " " + rs.getInt(7)+ " " + rs.getInt(8));
}
//5、关闭
rs.close();
cs.close();
ct.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
===========================================================================