下面是java操作数据库的模板,只需要写出简单的sql语句,再调用该模板中对应的方法,就能完成平时我们的数据库查询和更新操作了。
public class JdbcTemplate {
private Map<String,Connection>map=Collections.synchronizedMap(new
Hashtable<String,Connection>());
private static CopyOfJdbcTemplate jt;
private JdbcPool jpool=JdbcPool.getInstance();
private CopyOfJdbcTemplate(){
}
public static CopyOfJdbcTemplate getInstance(){
if(jt==null){
jt=new CopyOfJdbcTemplate();
}
return jt;
}
/*
所有查询“基类”
*/
public Object queryFormOriented(String sql,Object[]
args,PreparedStatementSetter pss,RowMapper rowmapper,Class clazz)throws Exception{
Object obj=null;
Connection conn=null;
try{
conn=jpool.getConnection();
PreparedStatement ps=conn.prepareStatement(sql);
pss.setPatameter(ps);//赋值
ResultSet rs=ps.executeQuery();
obj=rowmapper.mappObject(rs);//获取结果集
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
conn.close();
}
return obj;
}
/*
联合查询 以二维数组形式返回查询结果
*
* */
public Object[][]joinQuery(String sql,final Object[]args) throws Exception{
return (Object[][])queryFormOriented(sql,args,new
PreparedStatementSetter(){
public void setPatameter(PreparedStatement ps) throws
SQLException {
if(args!=null){
for(int i=0;i<args.length;i++){
ps.setObject(i+1, args[i]);
}
}
}
},new RowMapper(){
public Object mappObject(ResultSet rs) throws Exception {
int cols=rs.getMetaData().getColumnCount();
int rows=0;
List list=new ArrayList();
while(rs.next()){
List lt=new ArrayList();
for(int i=0;i<cols;i++){
lt.add(rs.getObject(i+1));
}
list.add(lt);
rows++;
}
Object [][]objs=new Object[rows][cols];
for(int i=0;i<objs.length;i++){
List col=(List)list.get(i);
for(int j=0;j<objs[i].length;j++){
objs[i][j]=col.get(j);
}
}
return objs;
}
},null);
}
/*
查询结果为一对象
*
*/
public Object queryFormObject(String sql,final Object[]args,final Class clazz)
throws Exception{
return queryFormOriented(sql,args,new PreparedStatementSetter(){
public void setPatameter(PreparedStatement ps) throws
SQLException {
if(args!=null)
for(int i=0;i<args.length;i++){//获取参数个数
ps.setObject(i+1, args[i]);
}
}
},new RowMapper(){
Object obj=null;
Method []methods=clazz.getMethods();
String fieldName=null;
String name=null;
public Object mappObject(ResultSet rs) throws Exception{
ResultSetMetaData rsm= rs.getMetaData();
int cols=rsm.getColumnCount();
System.out.println(cols);
while(rs.next()){
obj=clazz.newInstance();
for(int i=1;i<=cols;i++){
fieldName=rsm.getColumnName(i);//得到第几列然后将获得这列的值
name=FormUtil.getSetMethodName
(fieldName.toLowerCase());
for(int
j=0;j<methods.length;j++){
if(name.equals
(methods[j].getName())){
methods
[j].invoke(obj, rs.getObject(fieldName));
break;
}
}
}
}
return obj;
}
},clazz);
}
/*
查询结果为一个对象集合
*/
public List queryFromList(String sql,final Object[]args,final Class clazz) throws
Exception{
return (List) queryFormOriented(sql,args,new
PreparedStatementSetter(){
public void setPatameter(PreparedStatement ps) throws
SQLException {
// TODO Auto-generated method stub
if(args!=null)
for(int i=0;i<args.length;i++){//获取参数
个数
ps.setObject(i+1, args[i]);
}
}},new RowMapper(){
List list=null;
Method []methods=clazz.getMethods();
String fieldName=null;
String name=null;
public Object mappObject(ResultSet rs) throws
Exception {
// TODO Auto-generated method stub
ResultSetMetaData rsm= rs.getMetaData
();
int cols=rsm.getColumnCount();
while(rs.next()){
if(list==null){
list=new ArrayList();
}
Object obj=clazz.newInstance();
for(int i=1;i<=cols;i++){
fieldName=rsm.getColumnName(i);//得到第i列 然后将获得这列的值
name=FormUtil.getSetMethodName(fieldName.toLowerCase());
for(int
j=0;j<methods.length;j++){
if
(name.equals(methods[j].getName())){
methods[j].invoke(obj, rs.getObject(fieldName));
break;
}
}
}
list.add(obj);
}
return list;
}},clazz);
}
/*
查询结果为某个值
*
*/
public Object queryFormSingleResulte(String sql,final Object[]args,final Class
clazz) throws Exception{
return queryFormOriented(sql,args,new PreparedStatementSetter(){
public void setPatameter(PreparedStatement ps) throws
SQLException {
if(args!=null)
for(int i=0;i<args.length;i++){//获取参数
个数
ps.setObject(i+1, args[i]);
}
}},
new RowMapper(){
public Object mappObject(ResultSet rs)
throws Exception {
Object obj=null;
while(rs.next()){
obj=rs.getObject(1);
}
return obj;
}},clazz);
}
/*
* 查询结果为 一个值集合
*/
public List queryFormObjectList(String sql,final Object[]args,final Class clazz)
throws Exception{
return (List)queryFormOriented(sql,args,new PreparedStatementSetter
(){
public void setPatameter(PreparedStatement ps) throws
SQLException {
if(args!=null)
for(int i=0;i<args.length;i++){//获取参数
个数
ps.setObject(i+1, args[i]);
}
}},
new RowMapper(){
public Object mappObject(ResultSet rs)
throws Exception {
Object obj=null;
List list=null;
while(rs.next()){
if(list==null){
list=new
ArrayList();
}
obj=rs.getObject(1);
list.add(obj);
}
return list;
}},clazz);
}
/*************************更新操作*********************/
/*
由于更新操作涉及事务处理,所以必须保证一个更新整个过程中必须只有一
个连接对象
这里运用到Threadlocal模式
*/
public Connection getConnection() throws Exception{
Connection conn=map.get(Thread.currentThread().getName());
if(conn==null){
conn=jpool.getConnection();
map.put(Thread.currentThread().getName(), conn);
}
return conn;
}
public Transation beginTransation() throws Exception{
Connection conn=getConnection();
conn.setAutoCommit(false);//关闭连接自动提交功能
return new Transation(conn);
}
public void close() throws Exception{
Connection conn=getConnection();
if(conn!=null){
conn.close();
map.remove(conn);
}
}
/*
所有更新“基类”
*/
public int updateDB(String sql,final Object[]args,PreparedStatementSetter pss)
throws Exception{
int rows=0;
Connection conn=getConnection();
PreparedStatement ps=conn.prepareStatement(sql);
pss.setPatameter(ps);
rows=ps.executeUpdate();
return rows;
}
public int updateDB(String sql,final Object[]args) throws Exception{
return updateDB(sql,args,new PreparedStatementSetter(){
public void setPatameter(PreparedStatement ps) throws
SQLException {
if(args!=null){
for(int i=0;i<args.length;i++){
ps.setObject(i+1, args[i]);
}
}
}
});
}
public static void main(String[] arg) {
String sql="select count(*) from orders where time like "+ "'%-01-%'";
Object []args=new Object[]{"01","02"};
try {
CopyOfJdbcTemplate jt=CopyOfJdbcTemplate.getInstance();
//for(jt.queryFormObjectList(sql,null,null)){
Object [][]objs=jt.joinQuery(sql,null);
for(int i=0;i<objs.length;i++){
for(int j=0;j<objs[i].length;j++){
System.out.print(objs[i][j]+" ");
}
System.out.println();
}
//}
} catch (Exception e) {
e.printStackTrace();
}
}
}
分享到:
相关推荐
JdbcTemplate是spring-jdbc提供的数据库核心操作类,那对JdbcTemplate进行事务控制呢?
jdbcTemplate分页彻底解决,使用游标滚动jdbcTemplate分页彻底解决,使用游标滚动
使用Spring的JdbcTemplate实现分页功能
spring-jdbcTemplate实例工程
JdbcTemplate基本使用 JDBC已经能够满足大部分用户最基本的需求,但是在使用JDBC时,必须自己来管理数据库资源如:获取PreparedStatement,设置SQL语句参数,关闭连接等步骤。 JdbcTemplate是Spring对JDBC的...
Spring 框架 JdbcTemplate 类中查询方法介绍 JdbcTemplate 是 Spring 框架中 org.springframework.jdbc.core 包提供的 JDBC 模板类,它是核心类,其他模板类都是基于它封装完成的。JdbcTemplate 类主要提供四类方法...
JdbcTemplate 调用存储过程
简单的jdbcTemplate预编译、回调等
SpringJdbcTemplate封装工具包,包括规范model格式接口,封装SpringJdbcTemplate,实现分页,自适应多种数据库
使用Spring的JdbcTemplate调用Oracle的存储过程
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.support.rowset.ResultSetWrappingSqlRowSet; import org.springframework.jdbc.support.rowset.SqlRowSet; import org....
SpringBoot操作多数据源(JPA+JdbcTemplate)使用HikariCP数据源进行示例。
JdbcTemplate的jar包 内含: commons-logging-1.2.jar spring-beans-5.0.0.RELEASE.jar spring-core-5.0.0.RELEASE.jar spring-jdbc-5.0.0.RELEASE.jar spring-tx-5.0.0.RELEASE.jar 查询一行的内容,封装为...
1.针对SpringMVC注解的配置...2.Spring+JdbcTemplate事物管理 3.Spring+Hibernate事物管理 4.Spring实现DWR注解方式的应用配置 5.Spring配置拦截器 6.Spring通过拦截器实现防止重复提交实例 对学习,会起到非常好的效果
Spring中的JdbcTemplate,Spring中的的事务.。。。。。。。
springmvc整合JdbcTemplate框架
JdbcTemplate使用就是JdbcTemplate的使用 再说两遍JdbcTemplate使用就JdbcTemplate的使用 20字够了
本资源包含了两个子项目,分别进行了springboot+jpa+jdbcTemplate的多数据源独立事务配置和jta分布式事务配置,并针对不同的情况编写了事务配置测试接口,还演示了JPA的domain一对多自动生成数据库表且不生成数据库...