Java_jdbc 基础笔记之十三 数据库连接(DAO)

 

public class DAO {
    // INSERT, UPDATE, DELETE 操作都可以包含在其中
    public void update(String sql, Object... args) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JDBCTools.getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCTools.close(null, ps, conn);
        }

    }

    // 查询一条记录, 返回对应的对象
    public  T get(Class clazz, String sql, Object... args) {
        T entity = null;
        // 1获取Connection
        Connection conn = null;
        // 2 获取PreparedStatement
        PreparedStatement ps = null;
        // 3填充占位符
        ResultSet rs = null;
        try {
            conn = JDBCTools.getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            // 4 进行查询得到rs
            rs = ps.executeQuery();
            // 5若 ResultSet中有记录,
            // 准备一个MAP 键:存放列的别名,值:存放列的值
            Map values = new HashMap();
            // 6得到ResultSetMetaData对象
            ResultSetMetaData rmsd = rs.getMetaData();
            // 7处理ResultSet,把指针向下移动一个单位
            if (rs.next()) {
                // 8 由ResultSetMetaData 对象得到结果集中有多少列
                for (int i = 0; i < rmsd.getColumnCount(); i++) {
                    // 9由ResultSetMetaData得到每一列的别名,由rs得到每一列的值
                    String columnLabel = rmsd.getColumnLabel(i + 1);
                    Object columnValue = rs.getObject(i + 1);
                    // 10 填充Map
                    values.put(columnLabel, columnValue);
                }

            }
            if (values.size() > 0) {
                // 11 用反射创建Class对应的对象
                entity = clazz.newInstance();
                // 12 遍历Map对象,用反射填充对象的属性值:
                // 属性名为Map中的key 属性值为Map中的value
                for (Map.Entry map : values.entrySet()) {
                    String fieldName = map.getKey();
                    Object fieldValue = map.getValue();
                    BeanUtils.setProperty(entity, fieldName, fieldValue);

                }

            }

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

        return entity;

    }
}

 

 

public class DAOTest {
    DAO dao = new DAO();

    @Test
    public void testUpdate() {
        String sql = "INSERT INTO customers(name,email,birth)VALUES(?,?,?)";
        dao.update(sql, "XiaoWang", "[email protected]", new Date(
                new java.util.Date().getTime()));

    }

    @Test
    public void testGet() {
        String sql="SELECT flow_id flowId, type, id_card iDCard, "
                + "exam_card examCard, student_name studentName, "
                + "location, grade " + "FROM examstudent WHERE flow_id = ?";
        Student stu=dao.get(Student.class, sql, 9);
        System.out.println(stu);
    }
}

 

 

 

 

转: https://blog.csdn.net/YL1214012127/article/details/48350107

你可能感兴趣的:(Java_jdbc 基础笔记之十三 数据库连接(DAO))