存儲過程與存儲函數說明:存儲函數有返回值!存儲過程沒有返回值!
指存儲在數據庫中供所有用戶程序調用的子程序叫存儲過程、存儲函數。
什麼時候用存儲過程/存儲函數
原則:如果只有一個返回值,用存儲函數;否則,就用存儲過程。
1.創建存儲過程
用CREATE PROCEDURE命令建立存儲過程。語法如下:
create [or replace] PROCEDURE 過程名[(參數列表)]
AS
變量聲明
PLSQL子程序體;
1)存儲過程入門:
create or replace
procedure sayHello
as
begin
dbms_output.put_line('hello world!');
end;
cmd命令行執行程序:
2)給員工漲工資
create or replace
procedure addSal(eno in number)
as
begin
update emp2 set sal=sal*1.1 where empno=eno;
dbms_output.put_line('漲工資完畢');
end;
3)帶有輸出結果的存儲過程
create or replace
procedure queryEmpSal (eno in number,pname out VARCHAR2,psal out number)
as
begin
select ename ,sal into pname,psal from emp2 where empno=eno;
end;
測試上述代碼:
我們通過“從數據庫Scott斷開連接”這句話 可以判斷數據庫已經正常關閉-->因此數據庫已經幫我們自動提交了事務!
2.創建存儲函數
函數(Function)爲一命名的存儲程序,可帶參數,並返回一計算值。函數和過程的結構類似,但必須有一個RETURN子句,用於返回函數值。函數說明要指定函數名、結果值的類型,以及參數類型等。
語法如下:
CREATE[OR REPLACE] FUNCTION 函數名(參數列表)
RETURN 函數值類型
AS
變量聲明
PLSQL子程序體;
1)存儲函數入門:create or replace
function querySal(eno in number)
return number
as
vsal emp2.sal%type;
begin
select sal into vsal from emp2 where empno=eno;
return vsal;
end;
測試上述代碼:
2)帶有輸出參數的存儲函數
create or replace
function queryFuncEmpSal (eno in number,pname out VARCHAR2,psal out number)
return number
as
begin
select ename ,sal into pname,psal from emp2 where empno=eno;
return 1;
end;
================================================================================================
通過jdbc調用存儲過程和存儲函數
1)搭建環境
導入oracle的jar包:E:\app\Administrator\product\11.2.0\dbhome_1\jdbc\lib
2)寫工具類JDBCUtils
package cn.itcast.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBCUtils {
private static String jdbcUrl="jdbc:oracle:thin:@localhost:1521:orcl";
private static String driverClass="oracle.jdbc.OracleDriver";
private static String username="scott";
private static String password="169500";
/**
* 獲取數據庫連接
*
*/
public static Connection getConnection(){
try {
Class.forName(driverClass);//註冊驅動
return DriverManager.getConnection(jdbcUrl, username, password);
} catch (Exception e) {
throw new RuntimeException();
}
}
/**
* 關閉資源
* public
*/
public static void closeResource(Connection conn,Statement state,ResultSet rs){
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
conn=null;
}
}
if(state!=null){
try {
state.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
state=null;
}
}
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}finally{
rs=null;
}
}
}
}
2)測試存儲過程:
package cn.itcast.test;
import java.sql.CallableStatement;
import java.sql.Connection;
import oracle.jdbc.internal.OracleTypes;
import org.junit.Test;
import cn.itcast.util.JDBCUtils;
public class TestProcedure {
Connection con=null;
CallableStatement call=null;
@Test
public void test() throws Exception{
String sql="{call addSal(?)}";
con=JDBCUtils.getConnection();
call=con.prepareCall(sql);
call.setObject(1, 7369);
//設置參數
call.execute();//執行
//顯示結果
JDBCUtils.closeResource(con, call, null);
}
@Test
public void test1() throws Exception{
String sql="{call queryEmpSal(?,?,?)}";
con=JDBCUtils.getConnection();
call=con.prepareCall(sql);
//設置輸入參數
call.setObject(1, 7369);
//設置輸出參數
call.registerOutParameter(2, OracleTypes.VARCHAR);
call.registerOutParameter(3, OracleTypes.NUMBER);
call.execute();//執行
//顯示結果
String name = (String) call.getObject(2);
Double value = call.getDouble(3);
System.out.println(name+":"+value);
JDBCUtils.closeResource(con, call, null);
}
}
單元測試test1()
3)測試存儲函數-->關於sql的寫法去jdk中的
package cn.itcast.test;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import org.junit.Test;
import oracle.jdbc.internal.OracleTypes;
import cn.itcast.util.JDBCUtils;
public class TestFunction {
@Test
public void test() throws Exception{
Connection conn=null;
CallableStatement call=null;
String sql="{?=call querySal(?)}";
conn=JDBCUtils.getConnection();
call=conn.prepareCall(sql);
call.setObject(2,7934);
call.registerOutParameter(1, OracleTypes.NUMBER);
//執行結果
call.execute();
//獲取返回結果
Object object = call.getObject(1);
System.out.println(object);
}
}
注意:關於sql的寫法去jdk中的: