Co jest nie tak z moim kodem?Jak wstawić dane z mysql do combobox?
Próbuję wstawić dane z mysql do combobox w 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");
}
}
Czy model jest prawidłowo wypełniony? – Smutje
Tak, model jest w porządku, ArrayList groupNames = new ArrayList (); –
próbowałeś tego? 'DefaultComboBoxModel model = new DefaultComboBoxModel(); dla (String groupname: groupNames) { model.addElement (nazwa grupy); } ' Możesz umieścić swoje wyniki jeden po drugim w comboboxmodel. Być może lepiej jest zaimplementować DefaultComboBoxModel za pomocą metody '.toArray()' w twoich nazwach grup. – Rubinum