2012-04-27 39 views
0

我必須使用PostgreSQL,但是當嘗試讀取Java中的函數時,我遇到了一些問題。返回java中的SETOF記錄的函數=錯誤

我的功能:

CREATE OR REPLACE FUNCTION tiena.RecursosData7(x text, OUT id text, OUT valor text)RETURNS SETOF record 
AS 
' 
    SELECT recursodc.idrecursodc, recursodc.valorfonte 
    FROM tiena.recursodc 
    WHERE valorfonte=$1; 
' 
LANGUAGE 'sql'; 

然後在Java中,我試圖讀取功能是這樣的:

try { 
    if (AbrirConexao()) { 
     conn.setAutoCommit(false); 
     proc = conn.prepareCall("{ call tiena.recursosdata7(?,?, ?)}"); 
     proc.setString(1,"IG - SP"); 
     proc.registerOutParameter(2, Types.VARCHAR); 
     proc.registerOutParameter(3, Types.VARCHAR); 

     //proc.execute(); 
     //resSet = (ResultSet) proc.getObject(1); 
     resSet = proc.executeQuery(); 
     while(resSet.next()) 
     { 
      String id = resSet.getString(1); 
      String fonte = resSet.getString(2); 
      System.out.println("id : "+ id +", fonte: "+ fonte); 
     } 
     proc.close(); 
    } 

但我總是得到同樣的錯誤。

Erro : Nenhum resultado foi retornado pela consulta. 
org.postgresql.util.PSQLException: Nenhum resultado foi retornado pela consulta. 
     at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:274) 
     at LerArquivos.ConexaoBD.RecuperarIDRecurso2(ConexaoBD.java:117) 
     at upload_cg.Main.main(Main.java:24) 

我試着動參數,函數的位置和我搜索了很多,但我不找到解決方案。你有什麼建議嗎?

+0

如果你提到*你會得到什麼樣的錯誤,它肯定會有幫助。 – Voo 2012-04-27 20:14:44

+0

發佈錯誤StackTrace – 2012-04-27 20:15:49

+0

如果堆棧跟蹤(以及可能的類)使用的語言更爲廣泛,可以使用此社區(對不起,如英語)。 (出於好奇,語言是什麼,葡萄牙語?) – 2012-04-27 21:48:38

回答

0

Bellninita, 請考慮使用光標代替。下面是工作的例子:

protected Fault getFault(Integer buno, Integer faultCode, 
     GregorianCalendar downloadTime, IFilterEncoder filter, FaultType faultType, boolean verbose) { 
    Fault fault = new Fault(faultCode, 0); 
    try { 
     // We must be inside a transaction for cursors to work. 
     conn.setAutoCommit(false); 
     // Procedure call: getFault(integer, text, timestamp, integer) 
     proc = conn.prepareCall("{ ? = call getfaultCount(?, ?, ?, ?, ?) }"); 
     proc.registerOutParameter(1, Types.OTHER); 
     proc.setInt(2, buno); 
     proc.setInt(3, faultCode); 
     Timestamp ts = new Timestamp(downloadTime.getTimeInMillis()); 
     cal.setTimeZone(downloadTime.getTimeZone()); 
     proc.setTimestamp(4, ts, cal); 
     proc.setInt(5, filter.getEncodedFilter()); 
     proc.setString(6, faultType.toString()); 
     proc.execute(); 
     if(verbose) { 
      log.logInfo(this.getClass().getName(), "SQL: " + proc.toString()); 
     } 
     results = (ResultSet) proc.getObject(1); 
     while (results.next()) { 
      //Do something with the results here 
     } 
    } catch (SQLException e) { 
     //Log or handle exceptions here 
    } 
    return fault; 
} 

這裏是函數內部的SQL(又名存儲過程):

CREATE OR REPLACE FUNCTION getfaultcount(_bunoid integer, _faultcode integer, _downloadtime timestamp without time zone, _filterbitmap integer, _faulttype text) 
    RETURNS refcursor AS 
$BODY$ 
DECLARE mycurs refcursor; 
BEGIN 
    OPEN mycurs FOR 
    SELECT count(*) as faultcount, _downloadtime as downloadtime 
    FROM fs_fault f 
     JOIN download_time d ON f.downloadtimeid = d.id 
    WHERE f.faultcode = _faultcode 
     AND f.statusid IN(2, 4) 
     AND d.downloadtime = _downloadtime 
     AND d.bunoid = _bunoid 
    GROUP BY f.faultcode 
    ; 
    RETURN mycurs; 
END; 
$BODY$ 
    LANGUAGE plpgsql VOLATILE 
    COST 100; 
ALTER FUNCTION getfaultcount(integer, integer, timestamp without time zone, integer, text) OWNER TO postgres; 

雖然我的程序出現複雜的,它具有所有的基本組件,您將需要你的。我希望這對你有幫助,Bellninita。

+0

非常感謝,您給我的光標解決方案解決了我的問題=) – Bellninita 2012-04-27 21:49:01

+0

很高興聽到該解決方案適用於您,Bellninita。 – MAbraham1 2012-05-01 14:28:18