创建表:
create table myuser
(
id numeric(5,0) not null,
name varchar(20) not null,
sex varchar(8) not null
)
insert into myuser values(1 , '李亚希' , '男' );
创建存储过程
CREATE LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION getUsers()
RETURNS refcursor AS
$$
DECLARE recordcur refcursor;
BEGIN
OPEN recordcur FOR
SELECT id, name ,sex FROM myuser;
RETURN recordcur;
END;
$$LANGUAGE plpgsql;
这个文件我为了测试方便 放在 src 目录下
spring 配置文件
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>org.postgresql.Driver</value>
</property>
<property name="url">
<value>jdbc:postgresql://localhost:5432/postgres</value>
</property>
<property name="username">
<value>postgres</value>
</property>
<property name="password">
<value>postgres</value>
</property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
java 代码:
package test;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.jdbc.core.JdbcTemplate;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"dao.xml");
JdbcTemplate jdbcTemplate = (JdbcTemplate) context
.getBean("jdbcTemplate");
jdbcTemplate.execute(new CallableStatementCreator() {
public CallableStatement createCallableStatement(Connection con)
throws SQLException {
String storedProc = "{?=call getUsers()}";// 调用的sql
con.setAutoCommit(false); //这句很重要 , 没有报错
CallableStatement cs = con.prepareCall(storedProc);
cs.registerOutParameter(1, Types.OTHER);
return cs;
}
}, new CallableStatementCallback<Object>() {
public Object doInCallableStatement(CallableStatement cs)
throws SQLException, DataAccessException {
List<Map<String, String>> resultsMap = new ArrayList<Map<String, String>>();
cs.execute();
ResultSet rs = (ResultSet) cs.getObject(1);// 获取游标一行的值
while (rs.next()) {// 转换每行的返回值到Map中
Map<String, String> rowMap = new HashMap<String, String>();
rowMap.put("id", rs.getString("id"));
rowMap.put("name", rs.getString("name"));
rowMap.put("sex", rs.getString("sex"));
System.out.println(rowMap.get("id") + "========="
+ rowMap.get("name") + "==========="
+ rowMap.get("sex"));
}
rs.close();
return resultsMap;
}
});
}
}
执行结果:
1=========李亚希===========男
分享到:
相关推荐
postgres数据库odbc数据源 用于odbc连接postgres数据库
Postgres数据库基于持久内存的优化探索.pptx
postgres数据库linux版本
本资源使用python进行编写,解压后在pycharm中进行使用,该探查数据库适用于postgres数据库,运行结束后以.xlsx格式进行保存,探查的内容有:数据库名称、数据表名称、数据表注释、排序、字段名称、字段注释、字段...
postgres数据库中文手册,基本语言和编程规范
postgres数据库编程手册,html的文档,很好用的。
postgres数据库的存储过程函数,里面有3层循环,设计了很多内置函数,很有学习的价值,也可以改造做其他业务处理等,随时可解决问题
Postgres数据库SQL注入手册1
针对Postgres服务器端常用操作命令汇总 由于本人经常操作Postgres数据库养成笔记习惯 所以整理了一下针对数据库操作的各种命令
jmeter连接postgres数据库的样例
一个基于C#实现的Postgres数据库向导式创建的程序代码。
gadmin 是一个设计,维护和管理 Postgres 数据库用的通用工具。 它能在各种平台的Windows,Linux,FreeBSD,Mac和Solaris服务器上使用。 特性包括: 任意 SQL 语句。 可用于数据库,表,索引,序列,视图,触发器...
《在Sping Cloud(Spring Boot)中基于AbstractRoutingDataSource 实现多数据源动态切换》多数据源元数据存储表,在postgres数据库可直接运行,如果是别的数据库请自行修改sql语句
postgres数据库维护方法和恢复.docx
PostgreSQL数据库备份脚本,结合系统计划任务可以实现数据库的自动化备份。密码直接设置于脚本当中。
postgres数据库培训-EXPLAIN分析.pptx 详细介绍EXPLAIN分析
PostgresSQL是一个很优秀的开源数据库,并提供了针对空间数据的存扩展;这个PPT介绍了PostgresSQL数据库的基本情况
一个小 (~100 line) shell脚本用来管理Postgres数据库
针对两台服务器下的postgres数据库之间,实现无密码互相登录
一、数据库搭建 1、yum 指定目录安装 https://blog.csdn.net/llwy1428/article/details/105143053 2、yum 直接安装 https://blog.csdn.net/llwy1428/article/details/102486414 3、编译安装 ...4、PostgreSql 基本操作 ...