1

PostgreSQL を使用する必要がありますが、Java で関数を読み込もうとすると少し問題が発生します。

私の機能:

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)

パラメータ、関数の場所を移動しようとしましたが、よく検索しましたが、解決策が見つかりませんでした。何か提案はありますか?

4

2 に答える 2

0

Bellninita、代わりにカーソルの使用を検討してください。以下は、必要と思われるものと同様のコンポーネントを持つ Java および SQL コードの例です。これは、同じ目的で使用される製品コードに似ています。

protected Fault getFault(Integer dataCode, Integer faultCode,
        GregorianCalendar downloadTime, IFilterEncoder filter, FaultType faultType, boolean verbose) {
    // verbose: flag to display logging statements.
    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, dataCode);
        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(_dataCodeid 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.dataCodeid = _dataCodeid 
    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;
于 2012-04-27T21:27:08.483 に答える