0
我想使用SimpleJDBCCall使用REF_CURSOR調用PostgreSQL存儲過程。我的存儲過程是這樣的:使用SimpleJdbcCall和REF_CURSOR調用PostgreSQL存儲過程
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;
我試着用下面的Java代碼來調用程序:
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();
運行,我收到一條錯誤消息的代碼:
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)
我測試此代碼使用Oracele存儲過程,也使用REF_CURSOR。我使用方法代替withFunctionName()
。存儲過程被正確調用,並返回結果。
有沒有辦法調用使用SimpleJDBCCall返回REF_CURSOR的PostreSQL存儲過程?
雖然此代碼段可以是溶液,[包括一個解釋](// meta.stackexchange.com/questions/114762/explaining-entirely-基於代碼-答覆)確實有助於改善您的帖子的質量。請記住,您將來會爲讀者回答問題,而這些人可能不知道您的代碼建議的原因。 – peacetype