我想使用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()
withProcedureName()
方法而不是querySelector
。正确调用存储过程,并返回结果。
有没有办法调用使用SimpleJDBCCall返回REF_CURSOR的PostreSQL存储过程?
答案 0 :(得分:-1)
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));
}