• 利用反射完成初级万能DAO


    一、目标

    利用反射完成初级万能DAO

    二、注意

    1.Field[] fi = clazz.getDeclaredFields(); 

    for(Field ff : fi){
    ff.setAccessible(true);
    ff.set(ob, rs.getObject(ff.getName()));
    }

    三、代码

    package com.jikexueyuan.util;
    
    import java.lang.reflect.Field;
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.util.ArrayList;
    
    import com.jikexueyuan.bean.AnType;
    import com.jikexueyuan.bean.Animals;
    
    
    public class BaseDAO {
    	public ArrayList getList(Class cl){
    		ArrayList ar = new ArrayList();
    		Connection conn = BaseConnection.getConnection();
    		PreparedStatement ps = null;
    		ResultSet rs = null;
    		String sql = "select *  from "+cl.getSimpleName();
    		Field[] fi = cl.getDeclaredFields();
    		try {
    			ps = conn.prepareStatement(sql);
    			rs = ps.executeQuery();
    			while(rs.next()){
    				Object ob = cl.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 Object getObById(Class cl,int id){
    		Object ob = null;
    		Connection conn = BaseConnection.getConnection();
    		PreparedStatement ps = null;
    		ResultSet rs =null;
    		Field[] fi = cl.getDeclaredFields();
    		String sql = "select * from "+cl.getSimpleName()+" where "+fi[0].getName()+" = "+id;
    		try {
    			ps = conn.prepareStatement(sql);
    			rs = ps.executeQuery();
    			while(rs.next()){
    				ob = cl.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;
    	}
    	public ArrayList getListBySome(Class cl ,String name,Object value){
    		ArrayList ar = new ArrayList();
    		Connection conn = BaseConnection.getConnection();
    		PreparedStatement ps = null;
    		ResultSet rs = null;
    		Field[] fi = cl.getDeclaredFields();
    		String sql = "select * from "+cl.getSimpleName()+" where "+name+" = '"+value+"'";
    		try {
    			ps = conn.prepareStatement(sql);
    			rs = ps.executeQuery();
    			while(rs.next()){
    				Object ob = cl.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 boolean insert(Object ob){
    		boolean b = false;
    		Connection conn = BaseConnection.getConnection();
    		PreparedStatement ps = null;
    		Class cl = ob.getClass();
    		Field[] fi = cl.getDeclaredFields();
    		//insert into animals (name,age,anid) values(?,?,?)
    		String sql = "insert into "+cl.getSimpleName()+" (";
    		for(int i = 1;i<fi.length;i++){
    			sql = sql+fi[i].getName();
    			//4  0 1 2 3
    			if(i!=fi.length-1){
    				sql = sql+" , ";
    			}
    		}
    		sql = sql+") values (";
    		for(int i = 1;i<fi.length;i++){
    			sql = sql+" ? ";
    			if(i!=fi.length-1){
    				sql = sql+" , ";
    			}
    		}
    		sql = sql+")";
    		try {
    			ps = conn.prepareStatement(sql);
    			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){
    				b = true;
    			}
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}finally{
    			BaseConnection.closeRes(conn, ps);
    		}
    		return b;
    	}
    	public boolean insert1(Object ob){
    		boolean b = false;
    		Connection conn = BaseConnection.getConnection();
    		PreparedStatement ps = null;
    		Class cl = ob.getClass();
    		Field[] fi = cl.getDeclaredFields();
    		StringBuffer sb = new StringBuffer();
    		//insert into animals (name,age,anid) values(?,?,?)
    		sb.append("insert into ");
    		sb.append(cl.getSimpleName());
    		sb.append(" (");
    		for(int i = 1;i<fi.length;i++){
    			sb.append(fi[i].getName());
    			if(i!=fi.length-1){
    				sb.append(" , ");
    			}
    		}
    		sb.append(") values (");
    		for(int i = 1;i<fi.length;i++){
    			sb.append(" ? ");
    			if(i!=fi.length-1){
    				sb.append(" , ");
    			}
    		}
    		sb.append(" ) ");
    		try {
    			ps = conn.prepareStatement(sb.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){
    				b = true;
    			}
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    		}finally{
    			BaseConnection.closeRes(conn, ps);
    		}
    		return b;
    	}
    	
    	public boolean update(Object ob){
    		boolean b = false;
    		Connection conn = BaseConnection.getConnection();
    		PreparedStatement ps = null;
    		Class cl = ob.getClass();
    		Field[] fi = cl.getDeclaredFields();
    		StringBuffer sb = new StringBuffer();
    		//update animals set name = ?,age = ?,anid = ? where id = ?
    		sb.append(" update ");
    		sb.append(cl.getSimpleName());
    		sb.append(" set ");
    		for(int i = 1;i<fi.length;i++){
    			fi[i].setAccessible(true);
    			sb.append(fi[i].getName());
    			sb.append(" = ? ");
    			if(i!=fi.length-1){
    				sb.append(" , ");
    			}
    		}
    		sb.append(" where ");
    		sb.append(fi[0].getName());
    		sb.append("=?");
    		
    		try {
    			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();
    		}finally{
    			BaseConnection.closeRes(conn, ps);
    		}
    		return b;
    	}
    	
    	public boolean delete(Class cl , int id){
    		boolean b = false;
    		Connection conn = BaseConnection.getConnection();
    		PreparedStatement ps = null;
    		Field[] fi = cl.getDeclaredFields();
    		String sql = "delete from "+cl.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 boolean deleteBySome(Class cl , String name,Object value){
    		boolean b = false;
    		Connection conn = BaseConnection.getConnection();
    		PreparedStatement ps = null;
    		Field[] fi = cl.getDeclaredFields();
    		String sql = "delete from "+cl.getSimpleName()+" where "+name+" = ?";
    		try {
    			ps = conn.prepareStatement(sql);
    			ps.setObject(1, value);
    			int a = ps.executeUpdate();
    			if(a>0){
    				b = true;
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}finally{
    			BaseConnection.closeRes(conn, ps);
    		}
    	return b ;
    	
    	}
    	public static void main(String[] args) {
    		BaseDAO bd = new BaseDAO();
    //		Animals an = new Animals();
    //		an.setName("½ð¾ÅÅÖ");
    //		an.setAge(15);
    //		an.setAnId(1);
    //		an.setId(3);
    //		boolean b = bd.update(an);
    		
    		bd.deleteBySome(Animals.class, "name","½ð¾ÅÅÖ");
    		//bd.delete(Animals.class, 5);
    //		ArrayList<Animals> ar = bd.getList(Animals.class);
    //		for(Animals an : ar){
    //			System.out.println("±àºÅ£º"+an.getId()+"Ãû×Ö£º"+an.getName()+"ÄêÁä:"+an.getAge());
    //		}
    //		ArrayList<AnType> arr = bd.getList(AnType.class);
    //		for(AnType an : arr){
    //			System.out.println("±àºÅ£º"+an.getAnId()+"Ãû×Ö£º"+an.getAnName());
    //		}
    //		Animals an = (Animals)bd.getObById(Animals.class, 2);
    //		System.out.println(an.getName());
    //		ArrayList<Animals> ar = bd.getListBySome(Animals.class, "age", "13");
    //		for(Animals an : ar){
    //			System.out.println("±àºÅ£º"+an.getId()+"Ãû×Ö£º"+an.getName()+"ÄêÁä:"+an.getAge());
    //		}
    	}
    }
    

      源代码:http://files.cnblogs.com/files/shamgod/Java-DAO-4-v2.7z

    转自jikexueyuan

  • 相关阅读:
    第一次作业
    冯娟的第五次作业
    冯娟的第三次作业
    冯娟的第四次作业
    冯娟的第二次作业
    数据压缩第一次作业
    第二次实验
    第三次实验
    第一次实验
    第五次作业
  • 原文地址:https://www.cnblogs.com/shamgod/p/5014649.html
Copyright © 2020-2023  润新知