利用遊標返回結果集的的例子(Oracle 預存程序)

來源:互聯網
上載者:User
oracle|預存程序|遊標
在sqlplus中建立如下的內容:1、程式包

SQL> create or replace package types  2  as  3      type cursorType is ref cursor;  4  end;  5  /

程式包已建立。

2、函數SQL> create or replace function sp_ListEmp return types.cursortype  2  as  3      l_cursor    types.cursorType;  4  begin  5      open l_cursor for select id, title from cf_news order by id;--表的名字  6      return l_cursor;  7  end;  8  /

函數已建立。

3、過程

SQL> create or replace procedure getemps( p_cursor in out types.cursorType )  2  as  3  begin  4        open p_cursor for select id, title from cf_news order by id;--表的名字  5  end;  6  /

過程已建立。

4、建立一個可執行檔java控制台程式

import java.sql.*; import java.io.*; import oracle.jdbc.driver.*;  

class GetValues {   public static void main (String args [])                      throws SQLException, ClassNotFoundException   {       String driver_class = "oracle.jdbc.driver.OracleDriver";       String connect_string = "jdbc:oracle:thin:@127.0.0.1:1521:database";

      String query = "begin :1 := sp_listEmp; end;"; //此處調用前面建立的函數!      Connection conn;

      Class.forName(driver_class);       conn = DriverManager.getConnection(connect_string, "scott", "tiger");

      CallableStatement cstmt = conn.prepareCall(query);       cstmt.registerOutParameter(1,OracleTypes.CURSOR);       cstmt.execute();       ResultSet rset = (ResultSet)cstmt.getObject(1);

      while (rset.next ())         System.out.println( rset.getString (1) );         cstmt.close();   } }


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.