用mysql数据填充组合框 [英] Filling a combo box with mysql data

查看:77
本文介绍了用mysql数据填充组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

任何人都可以帮我吗.

推荐答案

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

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

这篇关于用mysql数据填充组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆