java 첨삭 JDBC 패키지 클래스 검사 (일반 패키지)

12086 단어 JDBC
    

package com.massky.connsqlserver;

import android.content.Context;
import android.content.SharedPreferences;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

//     
public class BaseDao {
    Class clazz;

    public BaseDao() {
        Type type = this.getClass().getGenericSuperclass();
        Type[] types = ((ParameterizedType) type).getActualTypeArguments();
        clazz = (Class) types[0];
    }


    //      
    public List queryList(T t) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String sql = getQuerySql(t);
        List list = new ArrayList();
        try {
            conn = DBUtilNew.getConn();
            ps = conn.prepareStatement(sql);
            common_excute(t, ps);
            rs = ps.executeQuery();
            ResultSetMetaData metaData = rs.getMetaData();
            int count = metaData.getColumnCount();
            while (rs.next()) {
                T obj = (T) clazz.newInstance();
                for (int i = 0; i < count; i++) {
                    String fieldName = metaData.getColumnName(i + 1);
                    Field field = clazz.getDeclaredField(fieldName);
                    Method method = clazz.getMethod(getSetter(fieldName), field.getType());
                    method.invoke(obj, rs.getObject(i + 1));
                }
                list.add(obj);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtilNew.close(rs, ps, conn);
        }
        return list;
    }


    //      
    public void deleteList(T t) {
        Connection conn = null;
        PreparedStatement ps = null;
        String sql = getDeleteSql(t);
        try {
            conn = DBUtilNew.getConn();
            ps = conn.prepareStatement(sql);
            common_excute(t, ps);
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtilNew.close(ps, conn);
        }
    }

    /**
     *       
     *
     * @param t
     * @param ps
     * @throws IllegalAccessException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     * @throws SQLException
     */
    private void common_excute(T t, PreparedStatement ps) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, SQLException {
        Field[] fields = clazz.getDeclaredFields();
        int c = 1;
        for (int i = 0; i < fields.length - 2; i++) {
            fields[i].setAccessible(true);
            Class> type = fields[i].getType();
            //  
            if (fields[i].get(t) != null) {
                if ("int".equals(type.getName()) && (int) fields[i].get(t) == -1)
                    continue;
                String fieldName = fields[i].getName();
                Method method = clazz.getMethod(getGetter(fieldName));
                Object obj = method.invoke(t);
                ps.setObject(c, obj);
                fields[i].setAccessible(false);
                c++;
            }
        }
    }

    /** 
          *   、  、  
          * @param sql 
          * @param params 
          * @return 
          * @throws SQLException 
          */
    public  boolean updateByPreparedStatement(String sql,List params) throws SQLException{
        Connection conn = null;
        PreparedStatement ps = null;
        boolean flag =false;
        int result = -1;
        try {
            conn = DBUtilNew.getConn();
            ps = conn.prepareStatement(sql);
            int index = 1;
            if (params != null && !params.isEmpty()) {
                for (int i = 0; i < params.size(); i++) {
                    ps.setObject(index++, params.get(i));
                }
            }

        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtilNew.close(ps, conn);
        }

        result = ps.executeUpdate();
        flag = result > 0 ? true : false;
        return  flag;
    }


    //    po
    public T queryPo(int id) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        T t = null;
        String sql = "select * from " + clazz.getSimpleName() + " where  id=?";
        try {
            t = (T) clazz.newInstance();
            conn = DBUtilNew.getConn();
            ps = conn.prepareStatement(sql);
            ps.setInt(1, id);
            rs = ps.executeQuery();
            ResultSetMetaData metaData = rs.getMetaData();
            int count = metaData.getColumnCount();
            while (rs.next()) {
                for (int i = 0; i < count; i++) {
                    String fieldName = metaData.getColumnName(i + 1);
                    Field filed = clazz.getDeclaredField(fieldName);
                    Method method = clazz.getMethod(getSetter(fieldName), filed.getType());
                    method.invoke(t, rs.getObject(i + 1));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtilNew.close(rs, ps, conn);
        }
        return t;
    }

    //       po   
    public int queryCount(T t) {
        return queryList(t).size();
    }



    //  po
    public void insertList(T t) {
        Connection conn = null;
        PreparedStatement ps = null;
        String sql = getInsertSql(t);
        try {
            conn = DBUtilNew.getConn();
            ps = conn.prepareStatement(sql);
            common_excute(t, ps);
            ps.execute();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtilNew.close(ps, conn);
        }
    }
//  po

    /**
     *     list
     *
     * @param t
     */
    public void updateList(T t, Map map) {
        Connection conn = null;
        PreparedStatement ps = null;
        String sql = getUpdateSql(t,map);
        try {
            conn = DBUtilNew.getConn();
            ps = conn.prepareStatement(sql);
            Field[] fields = clazz.getDeclaredFields();
            int c = 1;
            for (int i = 0; i < fields.length - 2; i++) {
                fields[i].setAccessible(true);
                Class> type = fields[i].getType();
                //  
                if (fields[i].get(t) != null) {
                    if ("int".equals(type.getName()) && (int) fields[i].get(t) == -1)
                        continue;
                    String fieldName = fields[i].getName();
                    Method method = clazz.getMethod(getGetter(fieldName));
                    Object obj = method.invoke(t);
                    ps.setObject(c, obj);
                    fields[i].setAccessible(false);
                    c++;//set  where     
                }
            }

            Set keys =  map.keySet();
            for (String key : keys) {
                ps.setObject(c,map.get(key));
                c++;//where and     
            }
//            ps.setInt(c, id);
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtilNew.close(ps, conn);
        }
    }

    //  
    public void deletePo(int id) {
        Connection conn = null;
        PreparedStatement ps = null;
        String sql = "delete from " + clazz.getSimpleName() + "  where id=?";
        System.out.println(sql);
        try {
            conn = DBUtilNew.getConn();
            ps = conn.prepareStatement(sql);
//            ps.setInt(1, id);
            ps.execute();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtilNew.close(ps, conn);
        }
    }

    //     sql  
    public String getQuerySql(T t) {
        String sql = "select * from " + "[" + clazz.getSimpleName() + "]";
        sql = get_common_sql(t, sql);
        return sql;
    }

    //     sql  
    public String getDeleteSql(T t) {
        String sql = "delete from  " + "[" + clazz.getSimpleName() + "]";
        sql = get_common_sql(t, sql);
        return sql;
    }


    /**
     *          
     *
     * @param t
     * @param sql
     * @return
     */
    private String get_common_sql(T t, String sql) {
        try {
            Field[] fields = clazz.getDeclaredFields();
            for (int i = 0; i < fields.length - 2; i++) {
                fields[i].setAccessible(true);
                Class> type = fields[i].getType();
                if (fields[i].get(t) != null) {
                    if ("int".equals(type.getName()) && (int) fields[i].get(t) == -1)
                        continue;
                    sql += "where 1=1 and " + fields[i].getName() + "=?";
                }
            }
            sql = sql.substring(0, sql.length());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sql;
    }

    //     sql  
    public String getUpdateSql(T t,Map map) {
        String sql = "update " + "[" + clazz.getSimpleName() + "]" + " set ";
        Field[] fields = clazz.getDeclaredFields();
        try {
            for (int i = 0; i < fields.length - 2; i++) {
                fields[i].setAccessible(true);
                Class> type = fields[i].getType();
                if (fields[i].get(t) != null) {
                    if ("int".equals(type.getName()) && (int) fields[i].get(t) == -1)
                        continue;
                    sql += fields[i].getName() + "=?,";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        sql = sql.substring(0, sql.length() - 1) + " where 1 = 1";
        //and " + fields[i].getName() + "=?"
        Set keys =  map.keySet();
        for (String key : keys) {
            sql += " and " +key + "=?";
        }
        return sql;
    }


    //     sql  
    public String getInsertSql(T t) {
        String sql = "insert into " + "[" + clazz.getSimpleName() + "]" + "(";
        String param = "";
        Field[] fields = clazz.getDeclaredFields();
        try {
            for (int i = 0; i < fields.length - 2; i++) {
                fields[i].setAccessible(true);

                Class> type = fields[i].getType();
                if (fields[i].get(t) != null) {
                    if ("int".equals(type.getName()) && (int) fields[i].get(t) == -1)
                        continue;
                    sql += fields[i].getName() + ",";
                    param += "?,";
                }
            }
            sql = sql.substring(0, sql.length() - 1) + ") values(" + param;
            sql = sql.substring(0, sql.length() - 1) + ")";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sql;
    }

    //  set      
    public static String getSetter(String fieldName) {
//        set    
        return "set" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);

    }

    //  get      
    public static String getGetter(String fieldName) {
//        set    
        String temp;
        temp = "get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
        return temp;


    }
}

좋은 웹페이지 즐겨찾기