从两个不同的mysql数据填充jcombobox

时间:2017-06-08 12:01:01

标签: java mysql jcombobox

我创建了一个带有员工列表表的jdialog,其中包含两个按钮,即添加和详细信息按钮。添加按钮将显示用于添加新员工的空白员工详细信息jdialog,而详细信息按钮将根据列表中的所选员工填充员工详细信息jdialog。问题是,当我单击详细信息时,部门的jcombobox不会显示所选员工的相应数据,而是仍显示部门列表。我想要的是,所述jcombobox必须首先显示部门列表之前所选员工的部门。我怎样才能做到这一点。以下是jcombobox的代码:

data = new ProcessDB();
        for(int i = 0; i < data.loadDeptCombo().get(0).size();i++){
            cmbDept.addItem(data.loadDeptCombo().get(1).get(i));
        }

public void loadInfo(String EmpID) throws ParseException{
    data = new ProcessDB();
    cmbDept.addItem(data.getEmpInfo(EmpID).get(4).get(0));
}

这是processdb:

public List<List<String>> loadDeptCombo(){
    sql = new String("SELECT * FROM departments ORDER BY Deptname ASC");
    List<List<String>> values =  new ArrayList<>();
    List<String> id = new ArrayList<>();
    List<String> dept = new ArrayList<>();

    try{
        stmt = data.createStatement();
        rs = stmt.executeQuery(sql);

        while(rs.next()){
            id.add(String.valueOf(rs.getInt("DeptID")));
            dept.add(rs.getString("Deptname"));
        }
    }catch(SQLException ex){
        ex.printStackTrace();
    }
    values.add(id);
    values.add(dept);

    return values;
}



 public List<List<String>> getEmpInfo(String EmpID){
    sql = new String("SELECT * FROM workinfo WHERE EmpID = ?");
    List<List<String>> values = new ArrayList<>();
    List<String> empID = new ArrayList<>();
    List<String> lastName = new ArrayList<>();
    List<String> firstName = new ArrayList<>();
    List<String> middleName = new ArrayList<>();
    List<String> department = new ArrayList<>();
    List<String> designation = new ArrayList<>();
    List<String> dateStarted = new ArrayList<>();
    List<String> rate = new ArrayList<>();
    List<String> section = new ArrayList<>();
    List<String> line = new ArrayList<>();
    List<String> admin = new ArrayList<>();

    try{
        pstmt = data.prepareStatement(sql);
        pstmt.setString(1, EmpID);
        rs = pstmt.executeQuery();

        while(rs.next()){
            empID.add(rs.getString("EmpID"));
            lastName.add(rs.getString("Lastname"));
            firstName.add(rs.getString("Firstname"));
            middleName.add(rs.getString("Middlename"));
            department.add(rs.getString("Department"));
            designation.add(rs.getString("Designation"));
            dateStarted.add(String.valueOf(rs.getDate("DateStarted")));
            rate.add(rs.getString("Rate"));
            section.add(rs.getString("Section"));
            line.add(rs.getString("Line"));
            admin.add(rs.getString("Admin"));
        }
    }catch(SQLException ex){
        ex.printStackTrace();
    }
    values.add(empID);
    values.add(lastName);
    values.add(firstName);
    values.add(middleName);
    values.add(department);
    values.add(designation);
    values.add(dateStarted);
    values.add(rate);
    values.add(section);
    values.add(line);
    values.add(admin);

    return values;
}

1 个答案:

答案 0 :(得分:0)

我通过安排这样的代码找到了我的解决方案:

if(EmpID != ""){
        try {
            data = new ProcessDB();
            loadInfo(EmpID);
            cmbDept.addItem(data.getEmpInfo(EmpID).get(4).get(0));
            cmbRate.addItem(data.getEmpInfo(EmpID).get(7).get(0));

            for(int i = 0; i < data.loadDeptCombo().get(0).size();i++){
                cmbDept.addItem(data.loadDeptCombo().get(1).get(i));
            }

            for(int i = 0;i < data.loadRateCombo().get(0).size();i++){
                cmbRate.addItem(data.loadRateCombo().get(1).get(i));
            }
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }else{
        try{
            data = new ProcessDB();
            for(int i = 0; i < data.loadDeptCombo().get(0).size();i++){
                cmbDept.addItem(data.loadDeptCombo().get(1).get(i));
            }

            for(int i = 0;i < data.loadRateCombo().get(0).size();i++){
                cmbRate.addItem(data.loadRateCombo().get(1).get(i));
            }

        }catch(Exception ex){
            //ex.printStackTrace();
            System.out.println(ex.getMessage());
        }

    }

如果字符串EmpID不为空,然后加载默认部门列表,则首先从表重新加载数据;如果为空,则仅重新加载组合框中的默认部门列表。