jdbctempleate 执行postgres 数据库存储过程返回结果集

创建表:
Sql代码 复制代码 收藏代码
  1. create table myuser
  2. (
  3. id numeric(5,0) not null,
  4. name varchar(20) not null,
  5. sex varchar(8) not null
  6. )
  7. insert into myuser values(1 , '李亚希' , '男' );


创建存储过程
Sql代码 复制代码 收藏代码
  1. CREATE LANGUAGE plpgsql;
  2. CREATE OR REPLACE FUNCTION getUsers()
  3. RETURNS refcursor AS
  4. $$
  5. DECLARE recordcur refcursor;
  6. BEGIN
  7. OPEN recordcur FOR
  8. SELECT id, name ,sex FROM myuser;
  9. RETURN recordcur;
  10. END;
  11. $$LANGUAGE plpgsql;

这个文件我为了测试方便 放在 src 目录下
spring 配置文件
Xml代码 复制代码 收藏代码
  1. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  2. <property name="driverClassName">
  3. <value>org.postgresql.Driver</value>
  4. </property>
  5. <property name="url">
  6. <value>jdbc:postgresql://localhost:5432/postgres</value>
  7. </property>
  8. <property name="username">
  9. <value>postgres</value>
  10. </property>
  11. <property name="password">
  12. <value>postgres</value>
  13. </property>
  14. </bean>
  15. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  16. <property name="dataSource">
  17. <ref local="dataSource"/>
  18. </property>
  19. </bean>

java 代码:
Java代码 复制代码 收藏代码
  1. package test;
  2. import java.sql.CallableStatement;
  3. import java.sql.Connection;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Types;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import org.springframework.context.ApplicationContext;
  12. import org.springframework.context.support.ClassPathXmlApplicationContext;
  13. import org.springframework.dao.DataAccessException;
  14. import org.springframework.jdbc.core.CallableStatementCallback;
  15. import org.springframework.jdbc.core.CallableStatementCreator;
  16. import org.springframework.jdbc.core.JdbcTemplate;
  17. public class Test {
  18. /**
  19. * @param args
  20. */
  21. public static void main(String[] args) {
  22. ApplicationContext context = new ClassPathXmlApplicationContext(
  23. "dao.xml");
  24. JdbcTemplate jdbcTemplate = (JdbcTemplate) context
  25. .getBean("jdbcTemplate");
  26. jdbcTemplate.execute(new CallableStatementCreator() {
  27. public CallableStatement createCallableStatement(Connection con)
  28. throws SQLException {
  29. String storedProc = "{?=call getUsers()}";// 调用的sql
  30. con.setAutoCommit(false); //这句很重要 , 没有报错
  31. CallableStatement cs = con.prepareCall(storedProc);
  32. cs.registerOutParameter(1, Types.OTHER);
  33. return cs;
  34. }
  35. }, new CallableStatementCallback<Object>() {
  36. public Object doInCallableStatement(CallableStatement cs)
  37. throws SQLException, DataAccessException {
  38. List<Map<String, String>> resultsMap = new ArrayList<Map<String, String>>();
  39. cs.execute();
  40. ResultSet rs = (ResultSet) cs.getObject(1);// 获取游标一行的值
  41. while (rs.next()) {// 转换每行的返回值到Map中
  42. Map<String, String> rowMap = new HashMap<String, String>();
  43. rowMap.put("id", rs.getString("id"));
  44. rowMap.put("name", rs.getString("name"));
  45. rowMap.put("sex", rs.getString("sex"));
  46. System.out.println(rowMap.get("id") + "========="
  47. + rowMap.get("name") + "==========="
  48. + rowMap.get("sex"));
  49. }
  50. rs.close();
  51. return resultsMap;
  52. }
  53. });
  54. }
  55. }  
原文地址:https://www.cnblogs.com/bjanzhuo/p/3575901.html