gpt4 book ai didi

java - 将数据库列添加到 JComboBox

转载 作者:行者123 更新时间:2023-12-01 23:48:30 25 4
gpt4 key购买 nike

我四处寻找这个问题的答案,但没有结果。当我编译它时,它只返回数据库中表的最后一行,而不是我期望的整个列的列表。我相信问题出在这儿..如果我能让它列出该栏中的所有内容,我将不胜感激您的帮助。

String query = "SELECT contact_id, first_name, last_name FROM my_contacts";
ResultSet rs = statement.executeQuery(query);

while (rs.next())
{
System.out.println(rs.getInt(1) + " " + rs.getString(2) + " " + rs.getString(3));
String name = rs.getString(2) + " " + rs.getString(3);
names = new JComboBox();
names.addItem(rs.getString("first_name"));
}//end while

最佳答案

When I compile this, it just returns the last row of my table in the database and not a list of the entire column as I expect. I believe the problem is from here..

while (rs.next())
{
System.out.println(rs.getInt(1) + " " + rs.getString(2) + " " + rs.getString(3));
String name = rs.getString(2) + " " + rs.getString(3);
names = new JComboBox();
names.addItem(rs.getString("first_name"));
}
  • 您的代码在 while (rs.next()){

    内的每个循环中创建了 JComboBox 的新实例
  • 创建 JComboBox 作为局部变量,然后只需将 while-loop 中的 Items 添加到已经存在并已初始化的实例

  • 最好的方法是使用 DeafultComboBoxModel添加/删除/修改项目JComboBox

关于java - 将数据库列添加到 JComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16651893/

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