gpt4 book ai didi

java - 如何将 mysql 中的数据插入组合框?

转载 作者:搜寻专家 更新时间:2023-11-01 00:56:35 25 4
gpt4 key购买 nike

我这里的代码有什么问题?

我正在尝试将数据从 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");
}
}

最佳答案

有时您尝试以这种方式使用模型或使用 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");
}
}

关于java - 如何将 mysql 中的数据插入组合框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24423779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com