CallableStatement PostgreSQL:参数数量错误错误

发布于 2024-12-17 16:56:43 字数 873 浏览 2 评论 0原文

我尝试在 postgresql 中编写示例存储函数,并使用 JDBC 提供的 CallableStatement 调用它们。

这是我的一些测试代码

Consumer bean =new Consumer();
CallableStatement pstmt = null;
try {
con.setAutoCommit(false);
String  query = "{ ? = call getData( ? ) }";
pstmt = con.prepareCall(query); 
 pstmt.registerOutParameter(1, Types.OTHER);
      pstmt.setInt(2,5);
      pstmt.execute(); // execute update statement
      bean=(Consumer)pstmt.getObject(1);
       System.out.println("bean"+bean.getConsumer_name());

,我的存储函数的形式为 。

CREATE FUNCTION getData(int) RETURNS SETOF db_consumer AS $$
 SELECT * FROM db_consumer WHERE consumer_id = $1;
$$ LANGUAGE SQL;

但是,当我尝试运行代码时出现以下错误。

org.postgresql.util.PSQLException: A CallableStatement was executed with an invalid    number of  parameters .

知道为什么会发生这种情况吗?

Im trying to write sample stored functions in postgresql and call them using the CallableStatement offered by JDBC.

Here's some my test code

Consumer bean =new Consumer();
CallableStatement pstmt = null;
try {
con.setAutoCommit(false);
String  query = "{ ? = call getData( ? ) }";
pstmt = con.prepareCall(query); 
 pstmt.registerOutParameter(1, Types.OTHER);
      pstmt.setInt(2,5);
      pstmt.execute(); // execute update statement
      bean=(Consumer)pstmt.getObject(1);
       System.out.println("bean"+bean.getConsumer_name());

And my Stored function is of the form .

CREATE FUNCTION getData(int) RETURNS SETOF db_consumer AS $
 SELECT * FROM db_consumer WHERE consumer_id = $1;
$ LANGUAGE SQL;

However, I'm getting the following error when I try to run the code .

org.postgresql.util.PSQLException: A CallableStatement was executed with an invalid    number of  parameters .

Any idea why this could be happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那一片橙海, 2024-12-24 16:56:43

我认为您不需要 CallableStatement,因为您应该能够直接运行 select * from getData(5)

PreparedStatement pstmt = con.prepareStatement("select * from getData(?)")
pstmt.setInt(1,5);
ResultSet rs = pstmt.execute(); 
while (rs.next()) {
  System.out.println(rs.getString(1));
}

I don't think you need a CallableStatement as you should be able to run select * from getData(5) directly:

PreparedStatement pstmt = con.prepareStatement("select * from getData(?)")
pstmt.setInt(1,5);
ResultSet rs = pstmt.execute(); 
while (rs.next()) {
  System.out.println(rs.getString(1));
}
橘味果▽酱 2024-12-24 16:56:43

您正在尝试通过 Callable 语句调用 SETOFF 函数。那不会发生!你总是会收到错误。

PostgreSQL 的存储函数可以通过两种不同的方式返回结果。该函数可以返回引用游标值或 SETOF 某种数据类型。根据使用的返回方法来确定应如何调用该函数。

以集合形式返回数据的函数不应通过 CallableStatement 接口调用,而应使用普通的 Statement 或PreparedStatement 接口。

You are trying to call a SETOFF function via a Callable Statement. That's not going to happen! You'll always get an error.

PostgreSQL's stored functions can return results in two different ways. The function may return either a refcursor value or a SETOF some datatype. Depending on which of these return methods are used determines how the function should be called.

Functions that return data as a set should not be called via the CallableStatement interface, but instead should use the normal Statement or PreparedStatement interfaces.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文