【发布时间】:2022-01-22 19:06:24
【问题描述】:
我们在 PostgreSQL 数据库中有一个存储过程,它接受多个输入和多个输出参数。当我们执行以下操作时,来自 PG Admin 客户端的过程调用可以正常工作,
调用 proc1(input1, input2, output1, output2)
但是,如果我们尝试通过 JDBC CallableStatement 进行此调用,则会收到以下错误,
org.postgresql.util.PSQLException: This statement does not declare an OUT parameter. Use { ?= call ... } to declare one.
at org.postgresql.jdbc.PgCallableStatement.registerOutParameter(PgCallableStatement.java:205)
PostgreSQL 驱动程序是“org.postgresql.Driver”
驱动版本是postgressql-42.2.5.jar
我们如何从 JDBC 调用具有多个输出参数的 PostgreSQL 过程?
请在下面找到代码sn-p,
public static void main(String args[]) throws SQLException {
Connection conn = null;
try {
String url = "jdbc:postgresql://<<hostname>>:<<port>>/<<DB>>";
Class.forName("org.postgresql.Driver");
Properties props = new Properties();
props.setProperty("user", "<<user>>");
props.setProperty("password", "<<pass>>");
conn = DriverManager.getConnection(url, props);
CallableStatement cs = conn.prepareCall("call schema.proc1(?,?,?,?)");
cs.setString(1, "test");
cs.setInt(2, 1000);
cs.registerOutParameter(3, Types.INTEGER);
cs.registerOutParameter(4, Types.VARCHAR);
cs.execute();
} catch (Exception e) {
log.error(e);
} finally {
if (conn != null) {
conn.close();
}
}
}
以下是程序的示例版本
Procedure proc1 is (input1 IN varchar2(10),
input2 IN number, output1 OUT number,
output2 OUT varchar2(10)) IS
BEGIN
output2 := input1;
output1 := input2;
END;
【问题讨论】:
-
请提供minimal reproducible example,其中包含您正在执行的存储过程的基本(简化)示例以及您用来执行的 Java 代码。
-
你确定程序是在 PostgreSQL 数据库中定义的吗?语法类似于
Oracle... -
是的,程序是从Oracle最近迁移到PostgreSQL的。 PostgreSQL 的源副本并不方便,因此添加了该过程的 Oracle 版本。从 pgAdmin 客户端运行时,PostgreSQL 过程没有问题并且运行良好。但是,当从 JDBC 调用时,它会引发上述错误。这似乎是对驱动程序的一些限制。
标签: java postgresql jdbc