利用游标返回结果集的的例子(Oracle 存储过程)JAVA调用方法和.NET调用方法

在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();   } }

---------------------==================----------------------------------------------------

本例在VS2005+Oracle 92010 + WindowsXp Sp2测试通过
1、创建一个游标变量,为返回值使用
create or replace package types as
  type cursorType is ref cursor;
end;
2、创建函数(或者存储过程)
create or replace function testpro return types.cursorType is
lc types.cursorType;
begin
  open lc for select * from test;
  return lc;
end testpro;
3、编写C#程序(注意:要先应用System.Data.OracleClient)
            OracleConnection conn = new OracleConnection("YourConnectString");
            OracleCommand cmd = new OracleCommand("testpro", conn);
            cmd.CommandType = CommandType.StoredProcedure;
 
            OracleParameter op = new OracleParameter("c", OracleType.Cursor);
            op.Direction = ParameterDirection.ReturnValue; 
            cmd.Parameters.Add(op);
 
            DataSet ds = new DataSet();
            OracleDataAdapter da = new OracleDataAdapter(cmd);
 
            da.Fill(ds,"test");
 
            this.dataGridView1.DataSource = ds.Tables["test"];
PS:使用储过程方法类似。
本例在VS2005+Oracle 92010 + WindowsXp Sp2测试通过
1、创建一个游标变量,为返回值使用
create or replace package types as
  type cursorType is ref cursor;
end;
2、创建函数(或者存储过程)
create or replace function testpro return types.cursorType is
lc types.cursorType;
begin
  open lc for select * from test;
  return lc;
end testpro;
3、编写C#程序(注意:要先应用System.Data.OracleClient)
            OracleConnection conn = new OracleConnection("YourConnectString");
            OracleCommand cmd = new OracleCommand("testpro", conn);
            cmd.CommandType = CommandType.StoredProcedure;
 
            OracleParameter op = new OracleParameter("c", OracleType.Cursor);
            op.Direction = ParameterDirection.ReturnValue; 
            cmd.Parameters.Add(op);
 
            DataSet ds = new DataSet();
            OracleDataAdapter da = new OracleDataAdapter(cmd);
 
            da.Fill(ds,"test");
 
            this.dataGridView1.DataSource = ds.Tables["test"];
PS:使用储过程方法类似。

原文地址:https://www.cnblogs.com/meimao5211/p/3381063.html