2016-07-11 3 views
0

Ich habe eine jComboBox, die Daten von MySQL-Server-Datenbank bekommen.Wie aktualisiert man jComboBox-Daten nach dem Update automatisch?

Wenn ich neue Daten zur Datenbank hinzufüge, zeigt die jComboBox es nicht an, und ich muss mein Programm erneut öffnen, um die neuen Daten zu jComboBox hinzuzufügen.

Wie kann ich jComboBox Daten automatisch aktualisieren?

Dies ist mein Code:

private void dataComboBox(){ 
    try { 
     Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop","root",""); 
     Statement stat = con.createStatement(); 
     String sql = "select id from perfume order by id asc";  
     ResultSet res = stat.executeQuery(sql);        
     while(res.next()){ 
      Object[] ob = new Object[3]; 
      ob[0] = res.getString(1); 
      jComboBox5.addItem(ob[0]);          
     } 
     } catch (Exception e) { 
      JOptionPane.showMessageDialog(null, e); 
    } 
} 


private void showCBdata(){ 
    try { 
     Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop","root",""); 
     Statement stat = con.createStatement(); 
     String sql = "select name from perfume where id='"+jComboBox5.getSelectedItem()+"'"; 
     ResultSet res = stat.executeQuery(sql); 

    while(res.next()){ 
     Object[] ob = new Object[3]; 
     ob[0]= res.getString(1);    
     jTextField8.setText((String) ob[0]); 
    } 
    } catch (Exception e) { 
     JOptionPane.showMessageDialog(null, e); 
    } 
} 


//call method 
private void jComboBox5ActionPerformed(java.awt.event.ActionEvent evt) {           
    showCBdata(); 
} 

können Sie mir helfen?

danke ..

Antwort

1

Sie es auf diese Weise tun können es die automatisch combobox

try { 
      comboBox.removeAllItems(); 

      sql = "SELECT * FROM `table_name`"; 
      rs = stmnt.executeQuery(sql); 

     while (rs.next()) { 
      String val = rs.getString("column_name"); 
      comboBox.addItem(val); 
     } 
    } catch (SQLException ex) { 
     Logger.getLogger(DefineCustomer.class.getName()).log(Level.SEVERE, null, ex); 
    } 

removeAllItems(); Methode, um die Combobox neu geladen sauber wird, dass, um sicherzustellen, keine Werte zu wiederholen.
Sie müssen kein separates Object erstellen, um stattdessen jComboBox hinzuzufügen. Sie können auch String hinzufügen.

0

Inzimam Tariq IT-Kodex (siehe oben):

try { 
      comboBox.removeAllItems(); 

      sql = "SELECT * FROM `table_name`"; 
      rs = stmnt.executeQuery(sql); 

     while (rs.next()) { 
      String val = rs.getString("column_name"); 
      comboBox.addItem(val); 
     } 
    } catch (SQLException ex) { 
     Logger.getLogger(DefineCustomer.class.getName()).log(Level.SEVERE, null, ex); 
    } 

Ich schlage vor, den gesamten Code innerhalb eines ActionListener setzen. Damit jedesmal dort die Maus über den comboBox eingegeben wird, wird der obige Code ausgeführt. Sie sollten folgendes tun:

public void mouseEntered(MouseEvent e) { 
     //the above code goes here 
    } 

Ich schlage vor, mit einer mouseListener: https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

Aber wenn man sich andere aussehen wollen ActionListeners können Sie hier sehen: https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

0

Sobald Sie hinzufügen neue Registrierung in der DB, ein removeAllItems comboBox.removeAllItems(); und repopulate de Combobox, Mein Beispiel:

jComboLicorerias.removeAllItems(); 

try { 
     Conector = Conecta.getConexion(); 
     Statement St = Conector.createStatement(); 
     try (ResultSet Rs = St.executeQuery(Query)) { 
      while (Rs.next()) { 
       jComboLicorerias.addItem(Rs.getString("nombreLicoreria")); 
      } 
      St.close(); 
     } 
    } catch (SQLException sqle) { 
     JOptionPane.showMessageDialog(null, "Error en la consulta."); 
Verwandte Themen