一:無返回值的存儲過程調用
存儲過程:
CREATE OR REPLACE PROCEDURE PRO_1(PARA1 IN VARCHAR2,PARA2 IN VARCHAR2) AS
BEGIN
INSERT INTO DBO.EMP (ID,NAME) VALUES (PARA1, PARA2);
END PRO_1;
Java代碼:
package com.icesoft.service;
import Java.sql.*;
import Java.sql.ResultSet;
public class CallProcedureTest1 {
public CallProcedureTest1() {
super();
}
public static void main(String[] args) {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:Oracle:thin:@127.0.0.1:1521:orcl ";
String user = "admin";
String pwd = "passWord";
Connection conn = null;
CallableStatement cs = null;
ResultSet rs = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, pwd);
cs = conn.prepareCall("{ call DBO.PRO_1(?,?) }");
cs.setString(1, "10");
cs.setString(2, "Peter");
cs.execute();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (cs != null) {
cs.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
}
}
} }
備注,存儲過程PRO_1中用到了表EMP(ID, NAME),需事先建好
二:有返回值的存儲過程(非結果集)
存儲過程:
CREATE OR REPLACE PROCEDURE PRO_2(PARA1 IN VARCHAR2,PARA2 OUT VARCHAR2) AS
BEGIN
SELECT INTO PARA2 FROM EMP WHERE ID= PARA1;
END PRO_2;
Java代碼:
package com.icesoft.service;
import Java.sql.*;
public class CallProcedureTest2 {
public CallProcedureTest2() {
super();
}
public static void main(String[] args) {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:Oracle:thin:@127.0.0.1:1521:orcl";
String user = "admin";
String pwd = "passWord";
Connection conn = null;
CallableStatement cs = null;
ResultSet rs = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, pwd);
cs = conn.prepareCall("{ call DBO.PRO_2(?,?) }");
cs.setString(1, "10");
cs.registerOutParameter(2, Types.VARCHAR);
cs.execute();
String name = cs.getString(2);
System.out.println("name: " + name);
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (cs != null) {
cs.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
}
}
}
注意:cs.getString(2)中的數值2並非任意的,而是和存儲過程中的out列對應的,如果out是在第一個位置,那就是proc.getString(1),如果是第三個位置,就是proc.getString(3),當然也可以同時有多個返回值,那就是再多加幾個out參數了。
三:返回列表
由於Oracle存儲過程沒有返回值,它的所有返回值都是通過out參數來替代的,列表同樣也不例外,但由於是集合,所以不能用一般的參數,必須要用pagkage了.所以要分兩部分,
1. 建一個程序包。如下:
CREATE OR REPLACE PACKAGE MYPACKAGE AS
TYPE MY_CURSOR IS REF CURSOR;
end MYPACKAGE;
2. 建立存儲過程,如下:
CREATE OR REPLACE PROCEDURE PRO_3(p_CURSOR out MYPACKAGE.MY_CURSOR) IS
BEGIN
OPEN p_CURSOR FOR SELECT * FROM DBO.EMP;
END PRO_3;
可以看到,它是把游標(可以理解為一個指針),作為一個out 參數來返回值的。
Java代碼:
package com.icesoft.service;
import Java.sql.*;
import Java.sql.ResultSet;
public class CallProcedureTest2 {
public CallProcedureTest2() {
super();
}
public static void main(String[] args) {
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:Oracle:thin:@127.0.0.1:1521:orcl";
String user = "admin";
String pwd = "passWord";
Connection conn = null;
CallableStatement cs = null;
ResultSet rs = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, user, pwd);
cs = conn.prepareCall("{ call DBO.PRO_3(?) }");
cs.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
cs.execute();
rs = (ResultSet) cs.getObject(1);
while (rs.next()) {
System.out.println("\t" + rs.getString(1) + "\t"
+ rs.getString(2) + "\t");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
if (cs != null) {
cs.close();
}
if (conn != null) {
conn.close();
}
}
} catch (SQLException e) {
}
}
}
}