我的代码有什么问题吗?即使数据库中有更多项目,第二个 JcomboBox 也仅显示一项。 它应该是这样工作的: 第一个 JcomboBox 填充来自数据库的数据,具体取决于用户在第一个组合框中的选择,填充第二个组合框,然后根据第二个 JcomboBox 中选择的项目填充一些 Jlabel 数据。
我尝试了 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();
}
}
最佳答案
您能否确认任何记录中“code”的值都不为空?从声音来看,第二条记录中“code”的值为 null(或任何其他无法解析为 int 的值),这导致了异常。
关于java - JcomboBox 仅显示一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10802825/