通过命令行创建存储过程
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;
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;
-- 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();
}
}
}
}
}
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