使用mysql数据填充组合框

时间:2011-06-14 09:46:25

标签: java mysql

大家好我有这个代码,但不知道为什么它不起作用!

//in database class 

String query = "SELECT group_name FROM customer ORDER BY group_name"; 
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; 
} 

任何人都可以帮助我..

1 个答案:

答案 0 :(得分:8)

您的问题不是很清楚,但是这里是如何使用从数据库中检索到的结果填充组合框:

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