2018-02-27 增删改查小框架实现

笔记如下
  • 需要导入的jar包
    c3p0-0.9.1.2.jar
    mysql-connector-java-5.1.45-bin.jar
  • 包结构


    2.png
3.png
  • DbAssist.java
    核心类

/*
 * 承诺:  有一个增删改的方法,一个用于完成查询的方法,还有个接口
 * 
 */
public class DBAssist {
    
    private DataSource ds;
    
    public DBAssist(DataSource ds) {
        
        this.ds = ds;
        
    }
    
    
    
    
    //这个方法完成增删改操作
    public void update(String sql,Object[] args) {
        
        Connection conn = null;
        PreparedStatement stmt = null;
        
        try {
            
            conn = ds.getConnection();
            
            stmt = conn.prepareStatement(sql);
            
            ParameterMetaData pmd = stmt.getParameterMetaData();
            
            //拿到占位符个数
            int count = pmd.getParameterCount();
            
            //替换占位符
            if (count>0) {
                
                //说明有占位符
                if(args == null) {
                    throw new IllegalAccessException("你的占位符有问提,不要乱搞");
                }
                if(count!=args.length) {
                    throw new IllegalAccessException("你的占位符个数不匹配,不要乱搞");
                }
                
                //for循环去替换占位符
                for (int i = 0; i < args.length; i++) {
                    
                    stmt.setObject(i+1, args[i]);
                    
                }
            }
            
            //执行操作
            stmt.executeUpdate();
            
            
        } catch (Exception e) {
            e.printStackTrace();
            //自定义异常 ,一般继承RuntimeException
            throw new MyRuntimeException(e);
            
        }finally {
            
            release(null, stmt, conn);
            
        }
    }
    
    
    
    //用这个方法实现查找
    public Object query(String sql,Object[] args,ResuletSetHandler rsh) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        
        try {
            
            conn = ds.getConnection();
            
            stmt = conn.prepareStatement(sql);
            
            ParameterMetaData pmd = stmt.getParameterMetaData();
            
            //拿到占位符个数
            int count = pmd.getParameterCount();
            
            //替换占位符
            if (count>0) {
                
                //说明有占位符
                if(args == null) {
                    throw new IllegalAccessException("你的占位符有问提,不要乱搞");
                }
                if(count!=args.length) {
                    throw new IllegalAccessException("你的占位符个数不匹配,不要乱搞");
                }
                
                //for循环去替换占位符
                for (int i = 0; i < args.length; i++) {
                    
                    stmt.setObject(i+1, args[i]);
                    
                }
            }
            
            //执行操作
            //需要告诉调用者自己去操作了....,所以要自己去定义一个接口
            rs = stmt.executeQuery();
            
            /*
             * 
             * 
             */
            return rsh.handle(rs);
            
            
        } catch (Exception e) {
            e.printStackTrace();
            //自定义异常 ,一般继承RuntimeException
            throw new MyRuntimeException(e);
            
        }finally {
            
            release(null, stmt, conn);
            
        }
        
        
    }
    
    
    
    //释放资源
    public static void release(ResultSet rs ,Statement stmt, Connection conn) {
        if(rs != null) {
            try {
                //释放获得的资源
                rs.close();
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }   
            
            //设置为空:斩断引用的对象
            //有利于回收垃圾
            rs = null;
            
        }
        
        if(stmt != null) {
            try {
                stmt.close();
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            stmt = null;
        }
        if(conn != null) {
            
            try {
                conn.close();
            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            
            conn = null;
            
        }

    }
    
    
}
  • MyRuntimeException.java
public class MyRuntimeException extends RuntimeException {

    public MyRuntimeException() {
        // TODO Auto-generated constructor stub
    }

    public MyRuntimeException(String message) {
        super(message);
        // TODO Auto-generated constructor stub
    }

    public MyRuntimeException(Throwable cause) {
        super(cause);
        // TODO Auto-generated constructor stub
    }

    public MyRuntimeException(String message, Throwable cause) {
        super(message, cause);
        // TODO Auto-generated constructor stub
    }

    public MyRuntimeException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
        // TODO Auto-generated constructor stub
    }

}
  • ResuletSetHandler.java
    查询接口
public interface ResuletSetHandler {

    Object handle(ResultSet rs);
        
}
  • BeanHandler.java
public class BeanHandler implements ResuletSetHandler {

    
    private Class clazz;
    public BeanHandler(Class clazz) {
        this.clazz = clazz;
        
    }
    
    
    
    @Override
    public Object handle(ResultSet rs) {
            
        
        try {
            
            ResultSetMetaData rsmd = rs.getMetaData();
            Object bean = clazz.newInstance();
            
            if(rs.next()) {
                //目标要封装的bean
                
                int columnCount = rsmd.getColumnCount();
                
                for (int i = 0; i < columnCount; i++) {
                    
                    //拿到当前i ,这列的值
                    Object value = rs.getObject(i+1);
                    //拿到列的名称
                    String columnName = rsmd.getColumnName(i+1);
                    
                    //由于列的名称和bean字段的类型和名称一致,所以可以使用反射去封装
                    //获得bean中相应的字段
                    Field declaredField = clazz.getDeclaredField(columnName);
                    
                    //打开访问权限
                    declaredField.setAccessible(true);
                    
                    declaredField.set(bean, value);
                    
                }
                
                
                
            }
            
            return bean;
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw new MyRuntimeException(e);
        }
    }

}

  • BeanListHandler.java
/*
 *  用于返回满足多个处理器封装的实现类.
 *  
 *  规定:你需要将封装数据到哪个javaBean上,你的
 * 
 */
public class BeanListHandler implements ResuletSetHandler {
    
    private Class clazz;
    public BeanListHandler(Class clazz) {
        // TODO Auto-generated constructor stub
        this.clazz = clazz;
        
    }
    
    
    
    @Override
    public Object handle(ResultSet rs) {
        
        List list = new ArrayList();
        
        
        try {
            
            ResultSetMetaData rsmd = rs.getMetaData();
            while(rs.next()) {
                //目标要封装的bean
                Object bean = clazz.newInstance();
                
                int columnCount = rsmd.getColumnCount();
                
                for (int i = 0; i < columnCount; i++) {
                    
                    //拿到当前i ,这列的值
                    Object value = rs.getObject(i+1);
                    //拿到列的名称
                    String columnName = rsmd.getColumnName(i+1);
                    
                    //由于列的名称和bean字段的类型和名称一致,所以可以使用反射去封装
                    //获得bean中相应的字段
                    Field declaredField = clazz.getDeclaredField(columnName);
                    
                    //打开访问权限
                    declaredField.setAccessible(true);
                    
                    declaredField.set(bean, value);
                    
                }
                
                list.add(bean);
                
            }
            
            return list;
            
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw new MyRuntimeException(e);
        }
        
        
        
    }

}

你可能感兴趣的:(2018-02-27 增删改查小框架实现)