2014-06-26 6 views
5

Cosa c'è di sbagliato nel mio codice qui?Come inserire dati da mysql nella casella combinata?

che sto cercando di inserire i dati del mysql nella casella combinata in netbean

private void btnSandoghMousePressed(java.awt.event.MouseEvent evt) {           
    try { 
     String query = "SELECT `AccountType` FROM `account`"; 
     con = Connect.ConnectDB(); 
     PreparedStatement stm = con.prepareStatement(query); 
     pst = con.prepareStatement(query);     
     ResultSet rs = pst.executeQuery(query); 
     ArrayList<String> groupNames = new ArrayList<String>(); 
     while (rs.next()) { 
      String groupName = rs.getString(4); 
      groupNames.add(groupName); 
     } 
     DefaultComboBoxModel model = new DefaultComboBoxModel(groupNames.toArray()); 
     cmbSemetarID.setModel(model); 
     rs.close();  
    } catch (SQLException e) { 
    System.err.println("Connection Error! it's about date"); 
    } 
} 
+0

Il modello è popolato correttamente? – Smutje

+0

Sì, il modello va bene, ArrayList groupNames = new ArrayList (); –

+1

hai provato questo? 'DefaultComboBoxModel model = new DefaultComboBoxModel(); per (Nome gruppo stringa: groupNames) { model.addElement (nomegruppo); } ' È possibile inserire i risultati uno a uno in comboboxmodel. Forse è meglio invece di avviare il DefaultComboBoxModel con il metodo '.toArray()' dei tuoi groupNames. – Rubinum

risposta

0

Si ottengono i problemi a volte si tenta di utilizzare modello in questo modo o utilizzando un Vector. Meglio cercare di fare qualcosa di simile,

private void btnSandoghMousePressed(java.awt.event.MouseEvent evt){           
    try { 
     String query = "SELECT `AccountType` FROM `account`"; 
     con = Connect.ConnectDB(); 
     PreparedStatement stm = con.prepareStatement(query); 
     pst = con.prepareStatement(query);     
     ResultSet rs = pst.executeQuery(query); 
     DefaultComboBoxModel model = new DefaultComboBoxModel(); 
     while (rs.next()) { 
      String groupName = rs.getString(4); 
      model.add(groupName); 
     } 

     cmbSemetarID.setModel(model); 
     rs.close();  
    } catch (SQLException e) { 
    System.err.println("Connection Error! it's about date"); 
    } 
} 
+0

leggi il mio commento su OP, post sbagliato, contro tutte le buone pratiche – mKorbel

0

Forse il metodo groupNames.toArray() non "adattarsi" nel Construktor DefaultComboBoxModel().

Si può cercare di mettere i vostri articoli nel vostro ArrayList uno per uno con questo:

DefaultComboBoxModel model = new DefaultComboBoxModel(); 
for(String groupname : groupNames) 
{ 
    model.addElement(groupname); 
} 
cmbSemetarID.setModel(); 

Ecco come mi riempiono la mia comboboxes.