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

Jos*_*ose 3 java oracle jdbc

我定义了以下存储过程:

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

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

call.execute();
Run Code Online (Sandbox Code Playgroud)

我猜它与第二个参数有关,它是一个 CLOB。

有谁知道我做错了什么?

谢谢你的帮助。

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) {}
    }
}
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

rge*_*man 5

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

如果您已经有一个Clob

call.setClob(1, clob);
Run Code Online (Sandbox Code Playgroud)

如果要将 a 转换String为 a Clob

call.setCharacterStream(1, new StringReader(string), string.length());
Run Code Online (Sandbox Code Playgroud)

如果要设置空 CLOB:

call.setNull(1, Types.CLOB);
Run Code Online (Sandbox Code Playgroud)

您还可以查看此解决方案