CallableStatement PostgreSQL:参数数量错误错误
我尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您不需要 CallableStatement,因为您应该能够直接运行
select * from getData(5)
:I don't think you need a CallableStatement as you should be able to run
select * from getData(5)
directly:您正在尝试通过 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.