JDBC连接数据库BaseDao通过反射书写

JDBC连接数据库BaseDao通过反射书写

村头老杨头

Fri Jul 3

村头老杨头
public class BaseDao<T>{
    private String url = "jdbc:mysql://127.0.0.1:3306/te?useUnicode=true&characterEncoding=utf8&useSSL=false&autoReconnect=true&failOverReadOnly=false";
    private String username = "root";
    private String password = "Spring123321";


    private Connection connection;
    private PreparedStatement preparedStatement;
    private ResultSet resultSet;

    //创建连接
    private Connection getConnection(){
        try {
	//加载驱动
            Class.forName("com.mysql.jdbc.Driver");
	//创建连接
            return DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    //增删改
    public int executeUpdate(String sql, Object ...obj){
	//获得连接
        connection = getConnection();
        try {
	    //sql预处理
            preparedStatement = connection.prepareStatement(sql);
	    //填充sql
            if(obj!=null){
                for (int i = 0; i < obj.length; i++) {
                    preparedStatement.setObject(i+1,obj[i]);
                }
            }
            int i = preparedStatement.executeUpdate();
            return i;

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return -1;
    }
    //查
    public List<T> executeQuery(String sql,Class cla,Object ...obj){
	//获得连接
        connection = getConnection();
        try {
	    //sql预处理
            preparedStatement = connection.prepareStatement(sql);
	    //填充sql
            if(obj!=null){
                for (int i = 0; i < obj.length; i++) {
                    preparedStatement.setObject(i+1,obj[i]);
                }
            }
	    //返回结果集
            resultSet = preparedStatement.executeQuery();
            //解析结果
            List arr = new ArrayList();
            while (resultSet.next()){
		//通过反射生成对象
                Object o = cla.newInstance();
		//获得对象属性数组
                Field[] fields = o.getClass().getDeclaredFields();
                for (Field field : fields) {
		    //获得结果集中的字段值
                    Object object = resultSet.getObject(field.getName());
		    //允许给对象属性赋值
                    field.setAccessible(true);
		    //给生成的o对象的属性赋值
                    field.set(o,object);
                }
		//创建的对象添加到数组中
                arr.add(o);
            }
            return arr;

        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } finally {
            close();
        }
        return null;
    }
    //关闭连接
    private void close(){
        if(connection!=null ){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(preparedStatement!=null){
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

评论