【发布时间】:2021-11-08 15:11:29
【问题描述】:
我正在从 Spring Boot 应用程序调用一个 postgres 存储过程,该应用程序具有 1 个文本类型的 IN 参数和 1 个 refcursor 类型的 INOUT 参数。如何从 CallableStatement 以外的 springboot 应用程序调用此过程。
public class CallProc {
public static void main(String[] args) throws ClassNotFoundException, java.sql.SQLException {
Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://azure.com/test";
Properties props = new Properties();
props.setProperty("user","test");
props.setProperty("password","test");
props.setProperty("ssl","true");
props.setProperty("escapeSyntaxCallMode", "callIfNoReturn");
Connection conn = DriverManager.getConnection(url, props);
// need a transaction
conn.setAutoCommit(false);
java.sql.CallableStatement callableStatement =
conn.prepareCall("{call myProc(?, ?)}");
callableStatement.setString(1, "user");
callableStatement.setObject(2, null);
callableStatement.registerOutParameter(2, java.sql.Types.REF_CURSOR);
callableStatement.execute();
java.sql.ResultSet rs =
(java.sql.ResultSet) callableStatement.getObject(2);
while (rs.next())
System.out.println(rs.getInt(1));
rs.close();
conn.commit();
conn.close();
}
}
而我的程序定义是这样的,
CREATE OR REPLACE PROCEDURE myapp.MyProc(
in_user_id TEXT,
INOUT user_roles refcursor)
language plpgsql
AS $BODY$
DECLARE
DERIVED USER_ID VARCHAR(50);
BEGIN
//body
...
//
END
$BODY$
谢谢
【问题讨论】:
-
为什么要以其他方式调用过程?
-
因为 CallableStatement 给出了 org.postgresql.util.PSQLException:错误:helloworld() 是一个过程 提示:要调用一个过程,请使用 CALL。这个错误@LaurenzAlbe 可以在这里检查 PG JDBC 驱动程序限制的限制highgo.ca/2020/11/25/…
标签: java postgresql stored-procedures