2012-04-27 3 views
0

PostgreSQL을 사용해야하지만 Java에서 함수를 읽으려고 할 때 약간의 문제가 있습니다.Java에서 SETOF 레코드를 반환하는 함수 =

내 기능 :

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

그러나 나는 항상 같은 오류가 발생합니다 :

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

그런 다음 자바에서 나는 기능이 방법을 읽을 것을 시도하고있다.

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

+0

오류 게시 StackTrace –

+0

스택 추적 (및 가능한 경우 클래스)이이 커뮤니티에서 더 널리 사용되는 언어 (미안, 영어와 같은) 인 경우 도움이됩니다. (호기심에서, 어떤 언어 _is_ that, portugese?) –

답변

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

+0

Bellninita가 해결책을 찾았습니다. – MAbraham1

관련 문제