zoukankan      html  css  js  c++  java
  • 万能的JDBC工具类。通过反射机制直接简单处理数据库操作

    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<fi.length;i++){
    			fi[i].setAccessible(true);
    			Sql.append(fi[i].getName());
    			Sql1.append("?");
    			if(i!= fi.length -1){
    				Sql.append(",");
    				Sql1.append(",");
    			}
    		}
    		Sql.append(") ").append("values (").append(Sql1).append(");");
    		try{
    			System.out.println(Sql.toString());
    			ps = conn.prepareStatement(Sql.toString());
    			for(int i =1;i<fi.length;i++){
    				fi[i].setAccessible(true);
    				ps.setObject(i, fi[i].get(ob));
    			}
    			int a = ps.executeUpdate();
    			if(a>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<fi.length;i++){
    			sb.append(fi[i].getName());
    			sb.append(" = ? ");
    			if(i!= fi.length -1){
    				sb.append(" , ");
    			}
    		}
    		sb.append(" where ");
    		sb.append(fi[0].getName()).append(" =?");
    		try{
    			System.out.println(sb.toString());
    			ps = conn.prepareStatement(sb.toString());
    			for(int i=1;i<fi.length ; i++){
    				fi[i].setAccessible(true);
    				ps.setObject(i, fi[i].get(ob));
    			}
    			fi[0].setAccessible(true);
    			ps.setObject(fi.length, fi[0].get(ob));
    			int a = ps.executeUpdate();
    			if(a>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;
    	}
    
    }
    

      

  • 相关阅读:
    SyncNavigator V8.6.2数据库同步工具安装与卸载
    数据库同步软件|SyncNavigator数据库同步软件 V8.6.2官网下载地址
    sql server两个数据库同步
    怎么实现数据库异地同步备份
    两台服务器数据库同步
    如何实现数据实时同步
    数据库同步软件|SyncNavigator数据库同步软件 V8.6.2
    syncnavigator使用说明
    go 编译
    Go语言实现FastDFS分布式存储系统WebAPI网关
  • 原文地址:https://www.cnblogs.com/firstdream/p/8672953.html
Copyright © 2011-2022 走看看