Java:resultset是空的,而调用存储过程,ref游标为OUT [英] Java: resultset is empty while calling stored procedure with ref cursor as OUT

查看:355
本文介绍了Java:resultset是空的,而调用存储过程,ref游标为OUT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试从Java调用StoredProcedure,但返回的结果总是 false 。在现实中,它必须返回100的记录。

I am trying to call a StoredProcedure from Java, but the result returned is always false. In reality, it has to return 100's of records. The connection is established good.

我有一个存储过程

 PROCEDURE get_records
   (
      grp1     IN a.name%TYPE DEFAULT NULL
     ,grp2 IN a.name%TYPE DEFAULT NULL
     ,grp3        IN a.name%TYPE DEFAULT NULL
     ,grp4          IN a.name%TYPE DEFAULT NULL
     ,grp5               IN a.name%TYPE DEFAULT NULL
     ,flag1     IN a.flag%TYPE DEFAULT 'F'
     ,flag2       IN a.flag%TYPE DEFAULT 'F'
     ,refercursor_out          OUT SYS_REFCURSOR
   );

我的java程式

import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

import oracle.jdbc.driver.OracleTypes;

class DAO1 {
    public static void main(String args[]) throws SQLException, IOException {
        // Load the driver
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        Connection conn = DriverManager
                .getConnection("jdbc:oracle:thin:admin/admin@//host1:1521/abcdev");
        System.out.println(conn);

        // Prepare to call the stored procedure get_group_details.
        CallableStatement cstmt = conn
                .prepareCall("{call mypackage.get_records (?,?,?,?,?,?,?,?)}");

        cstmt.registerOutParameter(8, OracleTypes.CURSOR); // REF CURSOR

        cstmt.setString(1, "");
        cstmt.setString(2, "");
        cstmt.setString(3, "");
        cstmt.setString(4, "");
        cstmt.setString(5, "");
        cstmt.setString(6, "");
        cstmt.setString(7, "");

        // execute get_records
        //cstmt.execute(); //updated
        cstmt.executeQuery(); //updated

        ResultSet rs0 = (ResultSet) cstmt.getObject(8);
        System.out.println("rs0 is " + rs0.next());

        /*
         * ResultSet rs = ((OracleCallableStatement) cstmt).getCursor(8);
         * System.out.println("rs is " + rs.next());
         * 
         * while (rs.next()) { System.out.println(".."); }
         */
        // Close the statement
        cstmt.close();
        // Close the connection
        conn.close();
    }
}

当前输出:

oracle.jdbc.driver.OracleConnection@5afec107
rs0 is false

问题 - 为什么是假的?我期望结果集中的100条记录。有来自sql开发人员执行测试脚本时可见的记录。

Question - why is it false? I expect 100's of records in the resultset. There are records, and is visible on executing the test script from sql developer.

begin
 mypackage.get_records(refercursor_out => :refercursor_out);
end;
// this returns 100's of records

更新:08/08

Update: 08/08

我没有获取数据的原因不是代码问题,当我传递一些值到第6个参数,它返回数据。因此关闭线程;标记@KlasLindbäck的答案作为建议正确的执行方法的答案。在这种情况下,execute(),executeQuery()和executeUpdate()会返回结果集中的数据]

The reason why I was not getting data is not the code issue, when I passed some value to the 6th parameter, it is returning data. Hence closing the thread; Marking @Klas Lindbäck's answer as the answer for suggesting the right execute method. [though execute(), executeQuery() and executeUpdate() in this case returns data in the resultset]

推荐答案

利用正确的语义。 execute 具有除 executeQuery 之外的其他语义。

You are not using the right semantics. execute has other semantics than executeQuery.


execute方法返回一个布尔值,表示第一个结果的形式。您必须调用方法getResultSet或getUpdateCount检索结果;您必须调用getMoreResults移动到任何后续结果。

The execute method returns a boolean to indicate the form of the first result. You must call either the method getResultSet or getUpdateCount to retrieve the result; you must call getMoreResults to move to any subsequent result(s).

最简单的解决方案到 executeQuery

    // execute get_records
    cstmt.executeQuery();

这篇关于Java:resultset是空的,而调用存储过程,ref游标为OUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆