首页 > 数据库 > Oracle > 正文

利用游标返回结果集的的例子(Oracle 存储过程)

2024-08-29 13:32:44
字体:
来源:转载
供稿:网友
菜鸟学堂:

在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();   } }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表