码迷,mamicode.com
首页 > 数据库 > 详细

怎样让Oracle的存储过程返回结果集

时间:2015-12-01 14:53:16      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:

Oracle存储过程:

 

 

CREATE OR REPLACE PROCEDURE getcity (
  citycode     IN       VARCHAR2,
  ref_cursor   OUT      sys_refcursor /* 这个sys_refcursor类型在SYS.STANDARD包中 */
)
AS
BEGIN
  OPEN ref_cursor FOR
    SELECT *
      FROM tb_city_code
     WHERE city_code = citycode;
END;
/

Java调用代码:

 

try {
          DriverManager.registerDriver (new oracle.jdbc.OracleDriver());
          Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:wjw", "sms", "sms");

          CallableStatement stmt = conn.prepareCall("BEGIN GETCITY(?, ?); END;");
          //CallableStatement stmt = conn.prepareCall("{ call GETCITY(?, ?) }");  //用此调用方法不能实现多行语法

          stmt.setString(1, "021");
          stmt.registerOutParameter(2, -10 /* OracleTypes.CURSOR = -10 */); //REF CURSOR(OracleTypes.CURSOR==-10)
          stmt.execute();
          ResultSet rs = (ResultSet) stmt.getObject(2);
          while (rs.next()) {
            System.out.println(rs.getString("CITY_NAME"));
          }
          rs.close();
          rs = null;
          stmt.close();
          stmt = null;
          conn.close();
          conn = null;
        }
        catch (SQLException e) {
          System.out.println(e.getLocalizedMessage());
        }

 

怎样让Oracle的存储过程返回结果集

标签:

原文地址:http://www.cnblogs.com/likeju/p/5009945.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!