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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14220954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 01:22:01  来源:igfitidea点击:

How do I use an IN OUT CLOB parameter defined in a Stored Proc in Oracle JDBC?

javaoraclejdbc

提问by Jose

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

现在我试图在 JDBC 中调用这个存储过程。但是,我收到一个异常,说“java.sql.SQLException: Parameter Type Conflict”。这是从行中提出的

call.execute();

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

我猜它与第二个参数有关,它是一个 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:

JDBC异常:

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)

回答by rgettman

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:

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

If you already have a Clob:

如果您已经有一个Clob

call.setClob(1, clob);

If you want to convert a Stringinto a Clob:

如果要将 a 转换String为 a Clob

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

If you want to set a null CLOB:

如果要设置空 CLOB:

call.setNull(1, Types.CLOB);

You can also see this solution.

您还可以查看此解决方案