简体   繁体   中英

Call PostgreSQL stored procedure using SimpleJdbcCall and REF_CURSOR

I want to call a PostgreSQL stored procedure with REF_CURSOR using SimpleJDBCCall. My stored procedure looks like:

CREATE OR REPLACE FUNCTION public.procName()
RETURNS refcursor AS
$BODY$
  DECLARE
  ref1 refcursor := 'cursor';
BEGIN
   open ref1  for SELECT *   FROM public."tableName";
   RETURN ref1;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION public.procName()
OWNER TO postgres;

I tried to call the procedure using the following JAVA code:

    JdbcTemplate jdbcTemplate = new JdbcTemplate(getDataSource());
    jdbcTemplate.setResultsMapCaseInsensitive(true);


    SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate);
    jdbcCall.withFunctionName("procName").returningResultSet("cursor",
            BeanPropertyRowMapper.newInstance(CustomEntity.class));

     Map m = jdbcCall.execute();

Running the code I received an error message:

org.postgresql.util.PSQLException: ERROR: cursor "cursor" does not exist
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2270)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1998)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:570)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:406)
at org.postgresql.jdbc2.AbstractJdbc2Connection.execSQLQuery(AbstractJdbc2Connection.java:339)
at org.postgresql.jdbc2.AbstractJdbc2ResultSet.internalGetObject(AbstractJdbc2ResultSet.java:213)
at org.postgresql.jdbc3.AbstractJdbc3ResultSet.internalGetObject(AbstractJdbc3ResultSet.java:36)
at org.postgresql.jdbc4.AbstractJdbc4ResultSet.internalGetObject(AbstractJdbc4ResultSet.java:312)
at org.postgresql.jdbc2.AbstractJdbc2ResultSet.getObject(AbstractJdbc2ResultSet.java:2711)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:457)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:413)
at org.springframework.jdbc.core.JdbcTemplate$6.doInCallableStatement(JdbcTemplate.java:1068)
at org.springframework.jdbc.core.JdbcTemplate$6.doInCallableStatement(JdbcTemplate.java:1066)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:1016)
at org.springframework.jdbc.core.JdbcTemplate.call(JdbcTemplate.java:1066)
at org.springframework.jdbc.core.simple.AbstractJdbcCall.executeCallInternal(AbstractJdbcCall.java:397)
at org.springframework.jdbc.core.simple.AbstractJdbcCall.doExecute(AbstractJdbcCall.java:369)
at org.springframework.jdbc.core.simple.SimpleJdbcCall.execute(SimpleJdbcCall.java:173)

I tested this code using an Oracele stored procedure, also with REF_CURSOR. I used instead of withFunctionName() the withProcedureName() method. The stored procedure is correctly called, and the result is returned.

Is there a way to call the PostreSQL stored procedue which is returning a REF_CURSOR using SimpleJDBCCall?

SimpleJdbcCall call = new SimpleJdbcCall(jdbcTemplate).withProcedureName("genidproc");
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("keystr", "uno");
Map<String, Object> result = call.execute(params);
for (String s : result.keySet()) {
     pw.println("6.0  " + result.get(s));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM