在Java中获取sys_Refcurson的结果

时间:2014-03-18 16:18:50

标签: java oracle stored-procedures

我有一个使用sys_Refcursor

的函数
create or replace function get_employee
  (p_loc in number)
  return sys_refcursor
as  
 l_rc sys_refcursor;
begin
  open l_rc
   for select a.first_name, a.last_name, b.department_name 
     from employees a, 
          departments b 
    where a.department_id=b.department_id 
      and location_id=p_loc;

 return l_rc; 
end;

并在SQL开发人员中运行

select (get_employee(5)) from dual;

在Java中,我使用了

strSql+="select (get_employee(5)) from dual";

        resultSet = selectStatement.executeQuery(strSql);


        while (resultSet.next()) {



            System.out.println("here is the " + resultSet.getString(1) );
            System.out.println("here is the " + resultSet.getString(2) );


        }

但是上面的代码将结果集返回为Null。

您能否告诉我如何在Java中检索该函数的结果值?

先谢谢

1 个答案:

答案 0 :(得分:0)

您无需执行查询即可获得红色咖喱。

create or replace function get_refcursor
return sys_refcursor
is
 c sys_refcursor;
begin
  open c for select 1 x from dual;
  return c;
end;


    DriverManager.registerDriver ( new oracle.jdbc.driver.OracleDriver());
    Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@****:1521:***","***","***");

    OracleCallableStatement cstmt = (OracleCallableStatement)conn.prepareCall("{ ? = call get_refcursor }"); ;
    cstmt.registerOutParameter(1, OracleTypes.CURSOR);
    cstmt.execute();
    ResultSet rs = (ResultSet)cstmt.getObject(1);
    while(rs.next()) {
        System.out.println('Result: '+rs.getBigDecimal(1));
    }
    rs.close();
    cstmt.close();

Result: 1