在sqlplus中建立如下的內容:
1、程式包
create or replace package types as type cursorType is ref cursor; end; /
程式包已建立。
2、函數
create or replace function sp_ListEmp return types.cursortype as l_cursor types.cursorType; begin open l_cursor for select id, title from cf_news order by id;--表的名字 return l_cursor; end; / 函數已建立。
3、過程
create or replace procedure getemps( p_cursor in out types.cursorType ) as begin open p_cursor for select id, title from cf_news order by id;--表的名字 end; / 過程已建立。
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(); } }