BaseDao

package com.sj.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 出来到对象没有这个方法需要的属性
                     */
            
                    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();
    }
    
    
    
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值