一個簡單例子描述了基本存儲過程的基本用法。
1、創建存儲過程 pro_stu
create or replace procedure pro_stu(iname in varchar2, ovalue out varchar2) as
r student%rowtype;
begin
select * into r from student s where s.name=iname;
ovalue:=r.value;
end;
在PL/SQL中調用存儲過程如下 :
declare
iname varchar2(20);
ovalue varchar2(20);
begin
iname:='&name';
pro_stu(iname,ovalue);
dbms_output.put_line('ovalue is '||ovalue);
end;
在Java中調用存儲過程如下
package com.kayak;
import Java.sql.CallableStatement;
import Java.sql.Connection;
import Java.sql.DriverManager;
import Java.sql.Types;
public class TestProc {
private static String sDriver = "oracle.jdbc.driver.OracleDriver";
private static String sUrl = "jdbc:Oracle:thin:@IP:1521:database";
private static Connection conn;
private static CallableStatement cs;
public static void main(String[] args) {
try {
Class.forName(sDriver);
conn = DriverManager.getConnection(sUrl, "username", "passWord");
cs = conn.prepareCall("{ call pro_stu(?,?) }");
cs.setString(1, "value1");
cs.registerOutParameter(2, Types.VARCHAR);
cs.execute();
String result = cs.getString(2);
System.out.println("返回結果為:" + result);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(1);
}
}