2012-04-27 9 views
0

PostgreSQLを使用する必要がありますが、Javaで関数を読み込もうとすると少し問題があります。java =エラーでSETOFレコードを返す関数

My機能:

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

はその後、Javaで私は機能をこのように読むことをしようとしています。

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

スタックトレース(および場合によってはクラス)がこのコミュニティによって広く使用されている言語(英語のように申し訳ありません)にあった場合にも役立ちます。 (奇妙なことに、何言語ですか?) –

答えて

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

関連する問題