2

我只是试图从服务器(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 循环中做同样的事情......并且没有显示更多内容。

也许问题很明显,但我错过了:(

谢谢!

4

1 回答 1

6

无需使用具有{call ...}语法的 CallableStatement。

只需使用 select 和常规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));
}
于 2012-12-02T10:29:01.910 回答