JDBC CallableStatement和Oracle SYS_REFCURSOR IN参数?

时间:2010-04-20 08:26:48

标签: java oracle jdbc cursor

它在标题中所说的内容,到底应该怎么做?

基本上我需要将一个null(空?)引用游标作为IN参数传递给存储过程。

/** spring's PreparedStatementSetter#setValues(...) */
public void setValues(PreparedStatement ps) throws SQLException {
    CallableStatement cs = (CallableStatement) ps;
    cs.setString(1,"constant");
    //this is the IN param
    cs.setNull(2, OracleTypes.CURSOR);
    //this is the OUT param
    cs.registerOutParameter(3, OracleTypes.CURSOR);
}

这失败了:

caused by: java.sql.SQLException: Unsupported feature: sqlType=-10
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
    at oracle.jdbc.driver.OraclePreparedStatement.setNullCritical(OraclePreparedStatement.java:4399)
    at oracle.jdbc.driver.OraclePreparedStatement.setNullInternal(OraclePreparedStatement.java:4161)
    at oracle.jdbc.driver.OracleCallableStatement.setNull(OracleCallableStatement.java:4472)

我在这里不知所措,尝试了几种不同的方法来做到这一点,但我找不到任何关于此的例子,也不知道如何正确地做到这一点。

1 个答案:

答案 0 :(得分:0)

我的java有点生疏,但你应该能够在PL / SQL块中初始化你的REF CURSOR变量:

String statement = "DECLARE\n" +
                       "l_cursor SYS_REFCURSOR;\n" +
                   "BEGIN\n" +
                       "your_procedure(?, l_cursor, ?);\n" +
                   "END;"
CallableStatement cs = yourConnection.prepareCall(statement);
// bind first and last parameter
cs.setString(1,"constant");
cs.registerOutParameter(2, OracleTypes.CURSOR);