Oracle學習第五天

存儲過程

1.存儲過程和存儲函數
指存儲在數據庫中供所有用戶程序調用的子程序叫存儲過程、存儲函數。
存儲過程和存儲函數的相同點:完成特點功能的程序
存儲過程和存儲函數的區別:是否用return語句返回值
2.創建和使用存儲過程
用create procedure命令建立存儲過程和存儲函數。
語法:
create [or replace] procedure 過程名(參數列表)as plsql子程序體;
調用存儲過程方式:
①exec salhelloworld();

begin 
sayhelloworld();
sayhelloworld();
end;
/


第一個存儲過程:Hello World
create or replace procedure salhelloworld
as
--說明部分
begin
dbms_output.put_line("Hello World");
end;
/
3.帶參數的存儲過程
例:爲指定的員工,漲100塊錢的工資;並且打印漲前和漲後的薪水;
create or replace procedure raisesalary(eno in number)
as
--定義變量保存漲前的薪水
psal emp.sal%type;
begin
--得到員工漲前的薪水
select sal into psal from emp where empno=eno;


--給該員工漲100
update emp set sal=sal+100 where empno=eno;
--需不需要commit?
--注意:一般不在存儲過程或者存儲函數中,commit和rollback。
--打印
dbms_output.put_line("漲前:"||psal||"漲後:"||(psal+100));

end;
/
4.調試存儲過程

存儲函數

1.簡介
函數(Function)爲一命名的存儲程序,可帶參數,並返回銥計算值。
函數和過程結構類型,但必須有一個return子句,用於返回函數值。
2.創建存儲函數的語法
create [or replace] function 函數名(參數列表)
return 函數值類型
as
plsql子程序體

例:查詢某個員工的年收入
create or replace function queryempincome(eno in number)
as
--定義變量保存員工的薪水和獎金
psal emp.sal%type;
pcomm emp.comm%type;
begin
--得到該員工的月薪和獎金
select sal,comm into psal,pcomm from emp where empno=eno;
--直接返回年收入
return psal*12+nvl(pcomm,0);
end;
/
3.in和out參數
存儲過程和存儲函數都可以有out參數
存儲過程和存儲函數都可以有多個out參數
存儲過程可以通過out參數來實現返回值

原則:如果只有一個返回值,用存儲函數;否則就用存儲過程。
例:查詢某個員工姓名,月薪和職位
create or replace procedure queryempinfo(eno in number,pename out varchar2,psal out number,pjob out varchar2);
as
begin
select ename,sal,empjob into pename,psal,pjob from emp where empno=eno;
end;
/
4.在應用程序中訪問存儲過程和存儲函數

private static String driver ="oracle.jdbc.OracleDriver";
private static String url="jdbc:oracle:thin:@192.168.56.101:1521:orcl";
private static String user="scott";
private static String password="tiger";


//註冊數據庫的驅動
static{
Class.forName(driver);//java的反射機制
//DriverManager.registerDriver(driver);
}
//獲取數據庫連接
public static Connection getConnection(){
return DriverManager.getConnection(url,user,password);
}

//釋放數據庫資源
public static void release(Connection conn,Statement st,ResultSet rs){
if(rs!=null){
rs.close();
}
finally{
rs=null;
}

if(st!=null){
st.close();
}
finally{
st=null;
}
if(conn!=null){
conn.close();
finally{
conn=null;
}
}
}


Class2
String sql="{call queryempinform(?,?,?,?)}";
Connection conn = null;
CallableStatement call = null;
//得到一個連接
conn = JDBCUtils.getConnection();
//通過連接創建出statment
call = conn.prepareCall(sql);
//對於in參數,賦值
call.setInt(1,7839);
//對於out參數,申明
call.registerOutParameter(2,OracleTypes.VARCHAR);
call.registerOutParameter(2,OracleTypes.NUMBER);
call.registerOutParameter(2,OracleTypes.VARCHAR);


//執行調用
call.execute();
//取出結果
String name = call.getString(2);
String sal = call.getDouble(3);
String job = call.getString(4);


JDBCUtils.Close();







String sql="{?=call queryempincome(?)}";
Connection conn = null;
CallableStatement call = null;
//得到數據庫連接
conn = JDBCUtils.getConnection();
//基於連接創建statement
call = conn.prepareCall(sql);

//對於輸出參數 申明
call.registerOutParameter(1,OracleType.NUMBER);
//對於輸入參數 賦值
call.setInt(2,7839);
//執行調用
call.execute();
//取出年收入的結果
double income = call.getDouble(1);


JDBCUtils.Close();

5.在out參數中使用光標
例:查詢某個部門中所有員工的所有信息

包頭:
create or replace
paceage mypackage as 
type empcursor is ref cursor;
procedure queryEmpList(dno in number,empList out empcursor);
end mypackage;


包體:
create or replace
package body mypackage as
procedure queryEmpList(dno in number,empList out empcursor) as
begin
--打開光標
open emplist for select * from emp where deptno=dno;
end queryEmpList;
end mypackage;


6.在應用程序中訪問包中的存儲過程

String sql="{call mypackage.queryEmpList(?,?)}";
Connection conn = null;
CallableStatement call = null;
ResultSet rs = null;
//獲取數據庫連接
conn =. JDBCUtilsgetConnection();
//創建statement
call = conn.prepareCall(sql);


//對於in參數,賦值
call.setInt(1,10);
//對於out參數,申明
call.registerOutParameter(2,OracleType.CURSOR);
//執行調用
call.execute();
//取出該部門中所有員工的信息
rs = ((OracleCallableStatement)call).getCursor(2);
while(rs.next()){
//取出該員工的員工號,姓名
int empno = rs.getInt("empno");
double salary = rs.getDouble("sal");
String job = rs.getString("empjob");
}
JDBCUtils.Close();
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章