Hai*_*tak 3 java swing jdbc jcombobox comboboxmodel
任何人都可以帮我解决这个代码吗?
在这里输入代码我想问一下,当从代码中的 jComboBox3 中选择另一个值时,如何清除 JcbSub(jComboBox):
1 private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {
2 Connection con;
3 Statement stmt;
4 try {
5
6 Class.forName("sun.jdbc.odbc.JdbcOdbc");
7 } catch (ClassNotFoundException ex) {
8 JOptionPane.showMessageDialog(null, ex);
9 }
10 try {
11 con= DriverManager.getConnection("Jdbc:Odbc:food");
12 stmt= con.createStatement();
13 String sql="select i_name from food where category= '"+ jComboBox3.getSelectedItem().toString()+"'";
14 ResultSet RS= stmt.executeQuery(sql);
15 JcbSub.setSelectedItem("");
16 while(RS.next()){
17
18 JcbSub.addItem(RS.getString("i_name"));
19
20
21 }
22
23
24
25 } catch (SQLException ex) {
26 Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
27 }
28
29
30
31 }
Run Code Online (Sandbox Code Playgroud)
小智 5
你必须这样做
private void jComboBox3ActionPerformed(java.awt.event.ActionEvent evt) {
Connection con;
Statement stmt;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbc");
} catch (ClassNotFoundException ex) {
JOptionPane.showMessageDialog(null, ex);
}
//add this to remove all Items
JcbSub.removeAllItems();
if(jComboBox3.getSelectedItem() == 0) {
try {
con= DriverManager.getConnection("Jdbc:Odbc:food");
stmt= con.createStatement();
String sql="select i_name from food where category= '"+jComboBox3.getSelectedItem().toString()+"'";
ResultSet RS= stmt.executeQuery(sql);
JcbSub.setSelectedItem("");
while(RS.next()){
JcbSub.addItem(RS.getString("i_name"));
}
} catch (SQLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}else if(jComboBox3.getSelectedItem() == 1) {
//etc...
}
// or use Switch case
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2278 次 |
| 最近记录: |