Filling a combo box with mysql data
hi everyone i have this code but don't know why it doesn't work!
//in database class
String query = "SELECT group_name FROM customer ORDER BY group_name";开发者_Python百科
java.sql.PreparedStatement stm = connection.prepareStatement(query);
rs = stm.executeQuery(query);
while (rs.next()) {
String x = rs.getString("group_name");
System.out.println(x);
}
rs.close();
}
//combo box action
int group = jcombobox.getSelectedIndex();
rg_domain rg = new rg_domain();
rg.setGroup(group);
rg.setPhone_number(phone_no);
dbconnection db = new dbconnection();
db.broadcastmsgservice_sms(rg);
}
//domain class
private String group;
public void setGroup(String group) {
this.group = group;
}
public String getGroup() {
return group;
}
can anyone help me please..
Your question is not very clear, but here's how you fill a combo box with results retrieved from the database:
// Create an array list to be filled with group names
ArrayList<String> groupNames = new ArrayList<String>();
String query = "SELECT group_name FROM customer ORDER BY group_name";
PreparedStatement stm = connection.prepareStatement(query);
ResultSet rs = stm.executeQuery(query);
while (rs.next()) {
String groupName = rs.getString("group_name");
// add group names to the array list
groupNames.add(groupName)
}
rs.close();
// Populate the combo box
DefaultComboBoxModel model = new DefaultComboBoxModel(groupNames.toArray());
comboBox.setModel(model);
精彩评论