2012-12-02 89 views
2

我只是試圖調用從服務器(getStat)存儲的功能,那就是尋找這樣的:現在,這裏PostgreSQL用於JDBC和存儲過程(功能):ResultSet的

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循環一樣的... ...並顯示僅此而已。

也許問題是顯而易見的事情,但我缺少:(

謝謝!

回答

6

沒必要用一個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)); 
} 
+0

這就是爲什麼我喜歡stackoverflow。你有一個程序化的問題?尋找快速和有效的幫助? 感謝馬沒有名字,工作很好:) – user1252280

+0

這對我真的很有幫助,謝謝! –