postgresql with jdbc and stored procedures (functions): ResultSet

别说谁变了你拦得住时间么 提交于 2019-12-04 14:38:38

问题


I just tried to call a stored function from the server (getStat), that is looking like this:

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';

Now here is the jdbc code:

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();
        }

Okay, and now the problem: When I am calling it, it just prints out the first line, of the whole bulk, that should be printend. Yeah, that is clear, because I execute the following code:

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

But I do the same in the while loop...and nothing more is displayed.

Maybe the problem is something obvious, but I am missing that :(

Thanks!


回答1:


No need to use a CallableStatement with the {call ...} syntax.

Just use a select and a regular 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));
}


来源:https://stackoverflow.com/questions/13668367/postgresql-with-jdbc-and-stored-procedures-functions-resultset

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!