儲存過程返回資料集

85579711發表於2014-03-27

一、定義包頭

    CREATE OR REPLACE  PACKAGE TEST_PACKAGE  is

    type testResultSet is ref cursor;
    end test_package;
    in string,  resultSet out test_package.testResultSet)
as
Begin
       open resultSet for
       select * from test;
End;
        try {
           CallableStatement pst = cnn.prepareCall("call TEST_PROCEDURE(?,?)");
           pst.setString(1, "11");
           pst.registerOutParameter(2, oracle.jdbc.OracleTypes.CURSOR);
           pst.execute();
           ResultSet set = (ResultSet)pst.getObject(2);
           while (set.next()) {
                System.out.println(set.getString(1));
           }
           pst.close();
           pst = null;
      } catch (Exception e) {
           e.printStackTrace();
      }
 }


二、編寫儲存過程

    CREATE OR REPLACE  PROCEDURE "RM"."TEST_PROCEDURE"  (templateid

三、編寫JAVA函式呼叫儲存過程

    public static void getValue() {
        try {
           CallableStatement pst = cnn.prepareCall("call TEST_PROCEDURE(?,?)");
           pst.setString(1, "11");
           pst.registerOutParameter(2, oracle.jdbc.OracleTypes.CURSOR);
           pst.execute();
           ResultSet set = (ResultSet)pst.getObject(2);
           while (set.next()) {
                System.out.println(set.getString(1));
           }
           pst.close();
           pst = null;
      } catch (Exception e) {
           e.printStackTrace();
      }
 }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29063131/viewspace-1130531/,如需轉載,請註明出處,否則將追究法律責任。

相關文章