2016-07-11 14 views
0

jComboBox私はMySQLサーバデータベースからデータを取得しています。更新後にjComboBoxデータを自動更新する方法は?

データベースに新しいデータを追加すると、jComboBoxに表示されず、新しいデータをjComboBoxに追加するためにプログラムを再度開く必要があります。

jComboBoxのデータは自動的にどのように更新できますか?

これは私のコードです:

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(); 
} 

あなたは私を助けることができますか?

あなたはこの方法でそれを行うことができます。..

答えて

1

ありがとう、それは自動的に更新されます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();方法は、値を繰り返さないことを保証するためにコンボボックスをきれいにします。
別のObjectを作成してjComboBoxに追加する必要はなく、Stringを追加することもできます。

0

(上記)Inzimamタリク・ITのコード:

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); 
    } 

私はActionListener内のこのコードのすべてを置くことをお勧めします。その都度、マウスをcomboBoxの上に入力すると、上記のコードが実行されます。あなたは、次の操作を行う必要があります。

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

私はmouseListenerを使用することをお勧め: https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

しかし、あなたは他のを見てみたい場合はActionListenersあなたがそれらをここに見ることができます: https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html

0

あなたが追加した後DB内の新しいレジストリ、removeAllItemsのcomboBox.removeAllItems();を実行します。コンボボックスを再投入すると、 私の例:

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."); 
関連する問題