如何使用Oracle存储函数在Java / JDBC中返回REF_CURSOR

时间:2015-03-18 19:13:11

标签: java oracle

我有以下代码,它不起作用!!你能帮我找出问题所在吗?我正在使用Oracle 11g。

        CallableStatement callableStatement = null;
    ResultSet rs = null;
    String getDBUSERCursorSql = "{? = call get_my_data(?,?)}";
    try {
        callableStatement = jdbcConn.prepareCall(getDBUSERCursorSql);

        callableStatement.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
        //--
        callableStatement.setDate(2, new java.sql.Date(someDate.getTime()));
        callableStatement.setString(3, userId);

        // execute getDBUSERCursor store procedure
        callableStatement.executeQuery();

        // get cursor and cast it to ResultSet
        rs = (ResultSet) callableStatement.getObject(1);

        while (rs.next()) {
            String userId = rs.getString("USER_ID");
            System.out.println("userId : " + userId);
        }
        ..
        ...
        ...

以下是错误:

java.sql.SQLException: ORA-06550: line 1, column 13:
PLS-00905: object SDB.GET_MY_DATA is invalid
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

这是存储功能:

CREATE OR REPLACE FUNCTION get_my_data (p_date   IN DATE,
                                    p_user_id       IN VARCHAR2)
 RETURN SYS_REFCURSOR
IS
 v_rc              SYS_REFCURSOR;
 BEGIN

     OPEN v_rc FOR
          SELECT *
            FROM user_logins
           WHERE login_date > p_date AND user_id = p_user_id
        ORDER BY created DESC;

  RETURN v_rc;
  END get_my_data;
  /

0 个答案:

没有答案