BaseDao

package com.zdyn.utils;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;

import java.util.List;


public class BaseDao {
	
	public static List findAll(Object obj,Connection conn) throws Exception{
		Class clazz=obj.getClass();
		Method[] m=clazz.getMethods();
		Field[] f=clazz.getDeclaredFields();
		List list=new ArrayList();
		String sql="select * from  "+obj.getClass().getSimpleName().toLowerCase();
		System.out.println(sql);
		//System.out.println(sql);
		PreparedStatement pst=conn.prepareStatement(sql);
		ResultSet rs=pst.executeQuery();
		while(rs.next()){
			Object obj2=clazz.newInstance();

			for(int i=0;i<f.length;i++){
				String n=f[i].getName();
				Object ob=rs.getObject(i+1);
				if(ob==null){
					ob="a";//��ֹ���Ϊnullʱ���ָ���쳣
				}
				if(ob.getClass().equals(java.lang.String.class)){
					
					/*
					 * ƴ��Set������
					 */
					for(int j=0;j<m.length;j++){
						if(("set"+n).equalsIgnoreCase(m[j].getName())){
							m[j].invoke(obj2, rs.getString(n));
						}
					}
					
				}else{
					for(int j=0;j<m.length;j++){
						if(("set"+n).equalsIgnoreCase(m[j].getName())){
							m[j].invoke(obj2, rs.getInt(n));
						}
					}
				}
				
			}
			list.add(obj2);
			
		}
		
		return list;
	}
	
		
	public static void save(Object obj,Connection conn) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, SQLException{
		Class clazz=obj.getClass();
		Method[] m=clazz.getDeclaredMethods();
		Field[] f=clazz.getDeclaredFields();

		String table=clazz.getSimpleName().toLowerCase();

		String sql="";
		for(int i=0;i<f.length;i++){
			sql=sql+f[i].getName()+",";
		}
		sql=sql.substring(0, sql.length()-1);
		String sql2="";
	
		for(int j=0;j<f.length;j++){
			
			String str="get"+f[j].getName();
			
			for(int k=0;k<m.length;k++){
				
				if(str.equalsIgnoreCase(m[k].getName())){
					//�жϡ����ط���bug
					/*
					 * new ��4������û��������Ҫ������
					 */
			
					Object p=m[k].invoke(obj);
			
					if(p==null){
						p="null";
						sql2=sql2+p+",";
					}else{
					sql2=sql2+"'"+p+"'"+",";
					}
				}
			}	
			
		}
		sql2=sql2.substring(0,sql2.length()-1);
		sql="insert into "+table+"("+sql+")"+" values"+"("+sql2+")";
		System.out.println(sql);
		PreparedStatement pst=conn.prepareStatement(sql);
		pst.executeUpdate();
	}
	
	
	
	
	public static void update(Object obj,Connection conn) throws Exception{
        //�޸�
        Class clazz=obj.getClass();
        Method[] m=clazz.getMethods();
        Field[] f=clazz.getDeclaredFields();
        /*
         * ƴ��Sql
         */
        String str1="";
        String str3="";
        for(int i=0;i<f.length;i++){
            //str1=str1+","+f[i].getName()+"="+"?";
            for(int j=0;j<m.length;j++){
                String str2="get"+f[i].getName();
                if(str2.equalsIgnoreCase(m[j].getName())){
                    str1=str1+f[i].getName()+"="+"'"+m[j].invoke(obj)+"'"+",";
                    if("getId".equalsIgnoreCase(m[j].getName())){
                     str3=m[j].invoke(obj).toString();
                    }
                }
            }
        }
        str1=str1.substring(0,str1.length()-1);
        String sql="update "+clazz.getSimpleName().toLowerCase()+" set "+str1+" where id="+"'"+str3+"'";
        System.out.println(sql);
      
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.execute();
       
    }
	
	
	
	
	public static void delById(Object obj,String id,Connection conn) throws SQLException{
		 Class clazz=obj.getClass();
		 String table=clazz.getSimpleName();
		 String sql="delete from "+table+" where id="+"'"+id+"'";
		 System.out.println(sql);
		 conn.prepareStatement(sql).executeUpdate();
	}
	
	
	
}

你可能感兴趣的:(BaseDao)