我的代码在这里出了什么问题?
我试图将mysql中的数据插入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 :(得分:0)
有时您会尝试以这种方式使用Model或使用Vector
时遇到问题。最好尝试做类似的事情,
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");
}
}
答案 1 :(得分:0)
也许你的groupNames.toArray()方法不适合DefaultComboBoxModel()构造函数。
您可以尝试将项目逐个放入ArrayList中:
DefaultComboBoxModel model = new DefaultComboBoxModel();
for(String groupname : groupNames)
{
model.addElement(groupname);
}
cmbSemetarID.setModel();
这就是我如何填充我的组合框。