import java.sql.*;
在乐东黎族等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供网站制作、成都网站建设 网站设计制作按需定制网站,公司网站建设,企业网站建设,高端网站设计,成都全网营销推广,成都外贸网站建设公司,乐东黎族网站建设费用合理。
import java.sql.ResultSet;
public class TestProcedureOne {
public TestProcedureOne() {
}
public static void main(String[] args ){
String driver = "oracle.jdbc.driver.OracleDriver";
String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
Statement stmt = null;
ResultSet rs = null;
Connection conn = null;
CallableStatement cstmt = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(strUrl,"orcl","orcl");
CallableStatement proc = null;
proc = conn.prepareCall("{ call orcl.package.procedure(?,?)}");
proc.setString(1, "100");
proc.setString(2, "TestOne");
proc.execute();//执行
}
catch (SQLException ex2) {
ex2.printStackTrace();
}
catch (Exception ex2) {
ex2.printStackTrace();
}
finally{
try {
if(rs != null){
rs.close();
if(stmt!=null){
stmt.close();
}
if(conn!=null){
conn.close();
}
}
}
catch (SQLException ex1) {
}
}
}
}
包用于在逻辑上组合过程和函数,它由包规范和包体两部分组成
1.创建包
--创建一个包sp_package
create package sp_package is
--声明该包有一个过程和函数,(没有实现)
procedure update_sal(name varchar2,newsal number);
function annual_nicome(name varchar2) return number;
end;
--创建包体(用于实现已经声明的函数和过程)
create package body sp_package is
procedure update_sal(name varchar2,newsal number)
is
BEGIN
UPDATE emp
SET sal = newsal
WHERE ename = name;
END;
function annual_income(name varchar2)
return number is
annual_salary number;
BEGIN
SELECT sal * 12 + Nvl(comm,0)
INTO annual_salary
FROM emp
WHERE ename = name;
RETURN annual_salary;
END;
end;
--调用包中的内容
exec sp_package.update_sal('name',number);
pl/sql里,select
语句必须接into
,不接into那就得返回一个游标出去,比如
create
or
replace
package
test_package
is
procedure
query_test(num
number,po_cursor
out
sys_refcursor);
end
test_package;
create
or
replace
package
body
test_package
is
procedure
query_test(num
number,po_cursor
out
sys_refcursor)
is
begin
select
unitname,tradetype,workernumber
from
test
where
income=num;
end
query_test;
end
test_package;
包用于在逻辑上组合过程和函数,它由包规范和包体两部分组成
1.创建包
--创建一个包sp_package
create package sp_package is
--声明该包有一个过程和函数,(没有实现)
procedure update_sal(name varchar2,newsal number);
function annual_nicome(name varchar2) return number;
end;
--创建包体(用于实现已经声明的函数和过程)
create package body sp_package is
procedure update_sal(name varchar2,newsal number)
is
BEGIN
UPDATE emp
SET sal = newsal
WHERE ename = name;
END;
function annual_income(name varchar2)
return number is
annual_salary number;
BEGIN
SELECT sal * 12 + Nvl(comm,0)
INTO annual_salary
FROM emp
WHERE ename = name;
RETURN annual_salary;
END;
end;
--调用包中的内容
exec sp_package.update_sal('name',number);