通過命令行創建存儲過程
create or replace procedure emp_sal(eno emp.empno%type,esal out emp.sal%type)
as
begin
select sal into esal from emp where empno=eno;
end;

PL/SQL 中測試存儲過程
依次打開 File -> New -> TestWindow 窗口,執行如下代碼:
-- Created on 2018/11/19 by ADMINISTRATOR
declare
-- Local variables here
v_sal emp.sal%TYPE;
begin
-- Test statements here
emp_sal(7369,v_sal);
dbms_output.put_line(v_sal);
end;


Java 中如何調用存儲過程
導入 jdbc 連接 oracle 的架包 ojdbc7
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;
public class TestJDBCProcedure {
public static void main(String[] args) {
Connection conn = null;
CallableStatement cs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:orcl", "scott", "123456");
cs = conn.prepareCall("call emp_sal(?,?)");
cs.setInt(1, 7369);
cs.registerOutParameter(2, Types.FLOAT);
cs.execute();
float sal = cs.getFloat(2);
System.out.println("薪資:" + sal);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (conn != null) {
try {
cs.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
運行結果
薪資:800.0
