博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
万能的JDBC工具类。通过反射机制直接简单处理数据库操作
阅读量:6716 次
发布时间:2019-06-25

本文共 6012 字,大约阅读时间需要 20 分钟。

package com.YY.util;import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.Properties;public class BaseConnection {	private static String conURL = "";	private static String cname = "";		private static String dbA = "";	private static String dbpassword = "";		static	{		try {			//获取外部配置的数据库链接信息			InputStream ips =BaseConnection.class.getClassLoader().getResourceAsStream("MyJDBC.properties");			Properties props = new Properties();			try {				props.load(ips);			} catch (IOException e) {				// TODO Auto-generated catch block				e.printStackTrace();			}			try {				ips.close();			} catch (IOException e) {				// TODO Auto-generated catch block				e.printStackTrace();			}			conURL = props.getProperty("conURL");			cname =	props.getProperty("cname");			dbA = props.getProperty("dbA");			dbpassword = props.getProperty("dbpassword");		} catch (Exception e) {			// TODO Auto-generated catch block			e.printStackTrace();		}	}	//	public static Connection getConnection() {        Connection conn = null;        try {            Class.forName(cname);            conn = DriverManager.getConnection(conURL, dbA, dbpassword);        } catch (Exception e) {            e.printStackTrace();        }        return conn;    }	public static void closeRes(Connection conn, PreparedStatement ps){		try {			if ( conn != null ) {				conn.close();			}					if ( ps != null ) {				ps.close();			}		} catch ( Exception e ) {			e.printStackTrace();		}	}	public static void closeRes(Connection conn, PreparedStatement ps,ResultSet rs){		try {			if ( conn != null ) {				conn.close();			}					if ( ps != null ) {				ps.close();			}						if ( rs != null ) {				rs.close();			}		} catch ( Exception e ) {			e.printStackTrace();		}	}}package com.YY.util;import java.lang.reflect.Field;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;public class BaseDAO {	//万能的获取全部内容	public static ArrayList getList(Class c1){		ArrayList ar = new ArrayList();		Connection conn = BaseConnection.getConnection();		PreparedStatement ps = null;		ResultSet rs = null;		String sql = "Select * from "+ c1.getSimpleName();		Field[] fi = c1.getDeclaredFields();		try{			ps = conn.prepareStatement(sql);			rs = ps.executeQuery();			while(rs.next()){				Object ob = c1.newInstance();//实例化类对象				for(Field ff :fi){					ff.setAccessible(true);					ff.set(ob, rs.getObject(ff.getName()));				}				ar.add(ob);			}		}catch(Exception e){			e.printStackTrace();		}finally{			BaseConnection.closeRes(conn, ps, rs);		}		return ar;	}		//万能的获取ID主键的一条数据。	public static Object getObById(Class c1,int id){		Object ob = null;		Connection conn = BaseConnection.getConnection();		PreparedStatement ps = null;		ResultSet rs = null;		Field[] fi = c1.getDeclaredFields();		String sql = "Select * from "+ c1.getSimpleName()+" where "+ fi[0].getName()+" = "+id;		try{			ps = conn.prepareStatement(sql);			rs = ps.executeQuery();			while(rs.next()){				ob = c1.newInstance();//实例化类对象				for(Field ff :fi){					ff.setAccessible(true);					ff.set(ob, rs.getObject(ff.getName()));				}			}		}catch(Exception e){			e.printStackTrace();		}finally{			BaseConnection.closeRes(conn, ps, rs);		}		return ob;	}	//万能的获取Where 条件后 的数据。	public static ArrayList getList(Class c1,String whereSql){		ArrayList ar = new ArrayList();		Connection conn = BaseConnection.getConnection();		PreparedStatement ps = null;		ResultSet rs = null;		String sql = "Select * from "+ c1.getSimpleName() +" "+ whereSql;		Field[] fi = c1.getDeclaredFields();		try{			ps = conn.prepareStatement(sql);			rs = ps.executeQuery();			while(rs.next()){				Object ob = c1.newInstance();//实例化类对象				for(Field ff :fi){					ff.setAccessible(true);					ff.set(ob, rs.getObject(ff.getName()));				}				ar.add(ob);			}		}catch(Exception e){			e.printStackTrace();		}finally{			BaseConnection.closeRes(conn, ps, rs);		}		return ar;	}		//插入的万能方法。	public static boolean Insert(Object ob){		boolean f = false;		Connection conn = BaseConnection.getConnection();		PreparedStatement ps = null;		Class c1 = ob.getClass();		Field[] fi = c1.getDeclaredFields();		StringBuffer Sql = new StringBuffer(); 		StringBuffer Sql1 = new StringBuffer();		Sql.append("insert into ").append(c1.getSimpleName()).append(" (");		for(int i =1 ;i
0){ f = true; } }catch(Exception e){ e.printStackTrace(); }finally{ BaseConnection.closeRes(conn, ps); } return f; } //万能更新 public static boolean update(Object ob){ boolean b = false; Connection conn = BaseConnection.getConnection(); PreparedStatement ps = null; Class c1 = ob.getClass(); Field[] fi = c1.getDeclaredFields(); StringBuffer sb = new StringBuffer(); sb.append("update ").append(c1.getSimpleName()).append(" set "); for(int i = 1; i
0){ b=true; } }catch(Exception e){ e.printStackTrace(); } return b; } //万能删除 public static boolean delete(Class c1 ,int id){ boolean b = false; Connection conn = BaseConnection.getConnection(); PreparedStatement ps = null; Field[] fi = c1.getDeclaredFields(); String Sql = "Delete from "+c1.getSimpleName()+" Where "+fi[0].getName()+" = ?"; try{ ps = conn.prepareStatement(Sql); ps.setObject(1, id); int a = ps.executeUpdate(); if(a>0){ b=true; } }catch(Exception e){ e.printStackTrace(); }finally{ BaseConnection.closeRes(conn, ps); } return b; } //万能删除 public static boolean delete(Class c1 ,String WhereSql){ boolean b = false; Connection conn = BaseConnection.getConnection(); PreparedStatement ps = null; Field[] fi = c1.getDeclaredFields(); String Sql = "Delete from "+c1.getSimpleName()+" "+WhereSql; try{ ps = conn.prepareStatement(Sql); int a = ps.executeUpdate(); if(a>0){ b=true; } }catch(Exception e){ e.printStackTrace(); }finally{ BaseConnection.closeRes(conn, ps); } return b; }}

  

转载地址:http://pqkmo.baihongyu.com/

你可能感兴趣的文章
我的友情链接
查看>>
关于vmware station 12pro 简易安装
查看>>
有用的正则表达式
查看>>
mysql show status解释
查看>>
Spark 下操作 HBase(1.0.0 新 API)
查看>>
PostgreSQL数据库切割和组合字段函数
查看>>
Jboss & Wildfly
查看>>
.NET简谈组件程序设计之(渗入序列化过程)
查看>>
DataGuard参数配置详解
查看>>
2010(Flex 初次使用 小节:No.2)
查看>>
VirtualBox 共享文件夹自动挂载
查看>>
PHP中使用PDO执行LIMIT语句无结果的问题
查看>>
apache日志统计工具
查看>>
查询rowid行数据所对应的数据对象编号,文件编号,块编号和行编号
查看>>
JSP数据库编程指南
查看>>
关于开发体系和开发过程
查看>>
配置管理小报101014:如何看wincvs的diff信息?
查看>>
消费者行为学
查看>>
构建HTTP中间件
查看>>
加速 npm
查看>>