我正在尝试在 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());
我的 Stored 函数的形式是 .
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 .
知道为什么会这样吗?
最佳答案
我认为您不需要 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));
}
关于java - 可调用语句 PostgreSQL :Invalid number of parameters error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8255299/