Java中的一对多ResultSet

时间:2018-08-20 06:47:42

标签: java

我的员工数据是:-

NAME | ID  | DEP_CODE
 AA   3679  SALES
 AA   3679  HR
 AA   3679  TECH
 AA   3679  MARKETTING
 BB   3604  HR
 BB   3604  SALES

有人可以修改它,并帮助我在这里消除FOR循环的需要,并一次性获取员工列表吗?我只想进行一次数据库调用,我想根据ID提取DEP_CODE。

public List<Employee> fetchDistinctEmployee() {

    List<Employee> empList = new ArrayList<Employee>();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("select distinct NAME, ID FROM Emp_Table");

        while (rs.next()) {
            Employee ep = new Employee();
            em.setNAME(rs.getString("NAME"));
            em.setID(rs.getInt("ID"));
            empList.add(em);
        }
        for (Employee emp : empList) {
            String sql1 = "SELECT DISTINCT DEP_CODE FROM Emp_Table WHERE id='" + emp.getID() + "'";
            ResultSet rs1 = stmt.executeQuery(sql1);
            ArrayList<String> list = new ArrayList<String>();
            while (rs1.next()) {
                list.add(rs1.getString("DEP_CODE"));
            }
            emp.setDEPCODE(list);
        }
    return empList;
}           

3 个答案:

答案 0 :(得分:2)

我会使用地图:

    Map<Integer,Employee> empMap = new LinkedHashMap<>();
    try (Statement stmt = conn.createStatement()) {
        ResultSet rs = stmt.executeQuery("select distinct NAME, ID, DEP_CODE FROM Emp_Table");

        while (rs.next()) {
            int id = rs.getInt("ID");
            Employee em = empMap.get(id);
            if (em == null) {
                em = new Employee();
                em.setID(id);
                em.setNAME(rs.getString("NAME"));
                em.setDEPCODE(new ArrayList<>()); // could be done in the constructor of Employee instead
                empMap.put(id, em);
            }
            em.addDEPCODE(rs.getString("DEP_CODE"));
        }
    }
    return new ArrayList<>(empMap.values());

方法addSEPCODE会将一个值添加到SEPCODE列表中。

请注意,顺便说一句,完成后关闭语句是个好主意。

答案 1 :(得分:1)

下面的代码仅是一个建议,希望对您有所帮助。请记住,这些代码尚未经过测试。

public List<Employee> fetchDistinctEmployee() {
    Map<String,Employee> empMap = new HashMap<String,Employee>();

    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery("select NAME, ID, DEP_CODE FROM Emp_Table");

    while (rs.next()) {
        String id = rs.getInt("ID");
        Employee ep = empMap.get(id);
        if(ep==null){
            ep = new Employee();
            empMap.put(id, ep);
            ep.setID(id);
            ep.setNAME(rs.getString("NAME"));
        }
        ep.getDEPCODE.add(rs.getString("DEP_CODE"));
    }
    return new ArrayList<Employee>(empMap.values());
}    

答案 2 :(得分:0)

只需修改OP的代码并仅循环一次:

public List<Employee> fetchDistinctEmployee() {

        List<Employee> empList = new ArrayList<Employee>();
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("select NAME, ID, DEP_CODE FROM Emp_Table sort by ID");

        Employee em = null ;
        while (rs.next()) {
            if(em == null || em.getID() != rs.getInt("ID")) {
                em = new Employee();
                em.setDEPCODE(new ArrayList<String>());
                empList.add(em);
            }
            em.setNAME(rs.getString("NAME"));
            em.setID(rs.getInt("ID"));
            em.getDEPCODE().add(rs.getString("DEP_CODE"));
        }
    return empList;
}