我的代码有什么问题?即使数据库中有更多项目,第二个 JcomboBox 也仅显示一项。它应该是这样工作的:第一个 JcomboBox 填充数据库中的数据,这取决于用户对第一个组合框的选择,第二个填充,然后一些 Jlabels 填充数据,具体取决于从第二个 JcomboBox 中选择的项目。
我试过 if(o == comboBox2) 仍然是同样的问题。
@Override
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
if(o == comboBox1) {
Object matricule = comboBox1.getSelectedItem();
String sqll = "Select * FROM clients WHERE matricule = " +matricule;
try {
rs = stat.executeQuery(sqll);
while(rs.next()) {
nom.setText(rs.getString("nom"));
prenom.setText(rs.getString("prenom"));
cin.setText(rs.getString("cin"));
adresse.setText(rs.getString("adresse"));
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
comboBox2.removeAllItems();
Object index = comboBox1.getSelectedItem();
String sql = "Select distinct p.code FROM parcelle p, clients c WHERE c.matricule = p.exploitant AND c.matricule = " +index;
try {
rs = stat.executeQuery(sql);
while(rs.next()) {
System.out.println(rs.getInt("code"));
comboBox2.addItem(rs.getInt("code"));
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return;
}
Object code = comboBox2.getSelectedItem();
String sqll = "Select * FROM parcelle WHERE code = " +code;
try {
rs = stat.executeQuery(sqll);
while(rs.next()) {
sau.setText(rs.getString("sau"));
sol.setText(rs.getString("type_sol"));
irrigation.setText(rs.getString("mode_irrigation"));
exploitation.setText(rs.getString("type_exploitation"));
}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}