轉載或別的時候,請拷貝 http://esc3721.blog.51cto.com/2366253/517800
關於Oracle存儲過程的介紹,我就不多囉嗦了。
建立存儲過程
- CREATE OR REPLACE PROCEDURE test_pro3(x OUT INT,y OUT int)
- IS
- BEGIN
- x := 20;
- y := 30;
- END;
查詢存儲過程:
- SQL> SELECT text FROM all_source WHERE TYPE = 'PROCEDURE' AND name = 'TEST_PRO3';
- TEXT
- --------------------------------------------------------------------------------
- PROCEDURE test_pro3(x OUT INT,y OUT int)
- IS
- BEGIN
- x := 20;
- y := 30;
- END;
- 已選擇6行。
JAVA調用存儲過程的code:
- package votory;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.*;
- import java.sql.ResultSet;
- public class ConneDB {
- public ConneDB() {
- }
- public static void main(String[] args) {
- String driver = "oracle.jdbc.driver.OracleDriver";
- String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521:SID";
- Statement stmt = null;
- ResultSet rs = null;
- Connection conn = null;
- CallableStatement cstmt = null;
- try {
- Class.forName(driver);
- conn = DriverManager.getConnection(strUrl, "scott", "tiger");
- CallableStatement proc = null;
- proc = conn.prepareCall("{ call test_pro3(?,?)}");
- proc.registerOutParameter(1, Types.INTEGER);
- proc.registerOutParameter(2, Types.INTEGER);
- proc.execute();
- String test = proc.getString(1);
- String test2 = proc.getString(2);
- System.out.println(test + " " + test2);
- System.out.println("DONE..............");
- } catch (SQLException ex2) {
- ex2.printStackTrace();
- } catch (Exception ex2) {
- ex2.printStackTrace();
- } finally {
- try {
- if (rs != null) {
- rs.close();
- if (stmt != null) {
- stmt.close();
- }
- if (conn != null) {
- conn.close();
- }
- }
- } catch (SQLException ex1) {
- }
- }
- }
- }
結果如下:
- 20 30
- DONE..............
如何定義和處理一個遊標呢:
- CREATE OR REPLACE PACKAGE testpackage
- IS
- TYPE test_cursor IS REF CURSOR;
- END;
- CREATE OR REPLACE PROCEDURE test_pro4(p_cursor OUT testpackage.test_cursor)
- IS
- BEGIN
- OPEN p_cursor FOR SELECT * FROM a;
- END;
總算是搞明白一個簡單例子。。。。。呵呵