且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用在Oracle JDBC的存储过程中定义的IN OUT CLOB参数?

更新时间:2023-11-06 16:49:28

我认为您不能直接将String传递给CLOB类型的参数.尝试绑定CLOB参数时,可以执行以下操作:

如果您已经有Clob:

call.setClob(1, clob);

如果要将String转换为Clob:

call.setCharacterStream(1, new StringReader(string), string.length());

如果要设置空的CLOB:

call.setNull(1, Types.CLOB);

您还可以看到此解决方案.>

I have the following stored procedure defined:

CREATE OR REPLACE PROCEDURE NOTIFY_INSERT (
  FLAG IN VARCHAR2,
  MESSAGE IN OUT CLOB,
  SEQ_NO OUT NUMBER
) 
AS
BEGIN
...
END;

Now I'm trying to call this stored proc in JDBC. However, I get an exception saying "java.sql.SQLException: Parameter Type Conflict". This is raised from the line

call.execute();

I'm guessing it has something to do with the second parameter, which is a CLOB.

Does anyone have an idea what I'm doing wrong?

Thanks for your help.

Connection connection = dataSource.getConnection();
CallableStatement call = null;
try {
    call = connection.prepareCall("{ call NOTIFY_INSERT (?,?,?) }");
    call.setString(1, flag);
    call.registerOutParameter(2, Types.CLOB);
    call.setString(2, message);
    call.registerOutParameter(3, Types.NUMERIC);
    call.execute();
    sequenceNo = call.getLong(3);
    message = call.getString(2);
} catch (SQLException e) {
    e.printStackTrace();
} finally {
    if (call != null) {
        try { call.close(); } catch (SQLException sqle) {}
    }
}

JDBC Exception:

java.sql.SQLException: Parameter Type Conflict
    at oracle.jdbc.driver.OraclePreparedStatement.processCompletedBindRow(OraclePreparedStatement.java:2480)
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:4356)
    at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:4595)
    at oracle.jdbc.driver.OracleCallableStatement.execute(OracleCallableStatement.java:10100)
    at oracle.jdbc.driver.OraclePreparedStatementWrapper.execute(OraclePreparedStatementWrapper.java:5693)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172)

I don't think that you can pass a String directly to a parameter of type CLOB. When attempting to bind a CLOB parameter, you can do the following:

If you already have a Clob:

call.setClob(1, clob);

If you want to convert a String into a Clob:

call.setCharacterStream(1, new StringReader(string), string.length());

If you want to set a null CLOB:

call.setNull(1, Types.CLOB);

You can also see this solution.