Search code examples
javasqlpostgresqljdbcjdbi

JDBI PostgreSQL: Get result of function from PSQL Call


I have a simple PSQL script

CREATE OR REPLACE FUNCTION foo(bar text) RETURNS text AS $$
    BEGIN
        --some stuff
        RETURN bar;
    END;
$$
LANGUAGE 'plpgsql';

And I want to extract the returned statement of

SELECT foo('hi') as result;

into my JDBI program in Java.

So currently I have this,

public String test() {
        Handle h = dbi.open();
        try {
            OutParameters ret = h.createCall("SELECT foo('hi') as result;").invoke();
            return ret.getString("result");
        } finally {
            h.close();
        }
    }

But I keep getting met with

Exception 'Parameter result does not exist'

What's the proper way of getting the results of PSQL function calls from JDBI?


Solution

  • http://jdbi.org/#_stored_procedure_calls

    It looks like JDBC has it's own format for outparameters in the form of

    OutParameters result = handle
            .createCall("{:sum = call add(:a, :b)}") 
            .bind("a", 13) 
            .bind("b", 9) 
            .registerOutParameter("sum", Types.INTEGER)   
            .invoke();