oracle 存儲過程使用 java

存儲過程爲:

CREATE OR REPLACE PROCEDURE SELTB(PARA1 IN VARCHAR2,PARA2 OUT VARCHAR2)  AS

BEGIN

   SELECT INTO PARA2 FROM TESTTB WHERE I_ID= PARA1;

END TESTB;

在java裏調用時就用下面的代碼:

public class TestProcedureTWO {

  public TestProcedureTWO() {

  }

  public static void main(String[] args ){

    String driver = "oracle.jdbc.driver.OracleDriver";

    String strUrl = "jdbc:oracle:thin:@127.0.0.1:1521:hyq";

    Statement stmt = null;

    ResultSet rs = null;

    Connection conn = null;

    try {

      Class.forName(driver);

      conn =  DriverManager.getConnection(strUrl, " hyq ", " hyq ");

      CallableStatement proc = null;

      proc = conn.prepareCall("{ call SELTB(?,?) }");//調用存儲過程

      proc.setString(1, "100");//這個是輸入參數

      proc.registerOutParameter(2, Types.VARCHAR);//將指定序號位置的 OUT 參數註冊爲給定的 JDBC 類型

      proc.execute();

      String testPrint = proc.getString(2);//獲取輸出參數,就是輸出參數所在的位置

      System.out.println("=testPrint=is="+testPrint);

    }

    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) {

      }

    }

  }

}

 

}

注意,這裏的proc.getString(2)中的數值2並非任意的,而是和存儲過程中的out列對應的,如果out是在第一個位置,那就是proc.getString(1),如果是第三個位置,就是proc.getString(3),當然也可以同時有多個返回值,那就是再多加幾個out參數了。

 

registerOutParameter (int, int)

將指定序號位置的 OUT 參數註冊爲給定的 JDBC 類型。

registerOutParameter (int, int, int)

將指定序號位置的 OUT 參數註冊爲給定的 JDBC 類型和小數位數。

registerOutParameter (int, int, java.lang.String)

將指定序號位置的 OUT 參數註冊爲給定的 JDBC 類型和類型名稱。

registerOutParameter (java.lang.String, int)

將具有指定名稱的 OUT 參數註冊爲給定的 JDBC 類型。

registerOutParameter (java.lang.String, int, int)

將具有指定名稱的 OUT 參數註冊爲給定的 JDBC 類型和小數位數。

registerOutParameter (java.lang.String, int, java.lang.String)

將具有指定名稱的 OUT 參數註冊爲給定的 JDBC 類型和類型名稱。

發佈了12 篇原創文章 · 獲贊 1 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章