用JDBC和存储过程(功能)的PostgreSQL:结果集(postgresql with jdbc

2019-08-03 10:18发布

我只是试图从服务器(getStat),就是这样看调用存储功能:

create type stat as (type text, location text, number int);
create function getStat() returns setof stat as 'select distinct table1.type, table1.location, table1.number from table1, table2 where table2.finding=10 order by number desc;' language 'sql';

现在这里是JDBC代码:

CallableStatement callable = null;

    String storedProc = "{call getStat(?, ?, ?)}";

    try {
        callable = connection.prepareCall(storedProc);

        callable.registerOutParameter(1, java.sql.Types.VARCHAR);
        callable.registerOutParameter(2, java.sql.Types.VARCHAR);
        callable.registerOutParameter(3, java.sql.Types.INTEGER);

        boolean results = callable.execute();

        System.out.println(callable.getString(1));
        System.out.println(callable.getString(2));
        System.out.println(callable.getInt(3));

        while(results){
            ResultSet rs = callable.getResultSet();
            while(rs.next()){
                System.out.println(rs.getString(1));
                System.out.println(rs.getString(2));
                System.out.println(rs.getInt(3));
            }
            //rs.close();

            results = callable.getMoreResults();
        }

好了,现在的问题:当我调用它,它只是打印出第一行,全散装的,应当printend。 是啊,这是明确的,因为我执行下面的代码:

            System.out.println(callable.getString(1));
            System.out.println(callable.getString(2));
            System.out.println(callable.getInt(3));

但我做同样的while循环......并显示仅此而已。

也许这个问题是显而易见的事情,但我缺少:(

谢谢!

Answer 1:

没必要用一个CallableStatement与{call ...}语法。

只需使用一个选择和常规Statement

Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("select * from getStat()");
while (rs.next())
{
  System.out.println(rs.getString(1));
  System.out.println(rs.getString(2));
  System.out.println(rs.getInt(3));
}


文章来源: postgresql with jdbc and stored procedures (functions): ResultSet