我想从我的数据库中得到一个有2个字段的结果集。
rs=Con.sqlQueryTable("Select id_prov, name_prov from prov");
然后我想在comboBox中显示名为“name_prov”的字段(作为项目)。但是我还想把我的“id_prov”,即ID(主键)作为这个项目的值。这样做的目的是通过使用comboBox将名称(在本例中是提供者的名称)与其ID关联起来。
这是我目前使用的JComboBox事件FocusGained的代码。
try {
//CBProv is the Combobox
CBProv.removeAllItems();
rs=Con.sqlQueryTable("Select id_prov, name_prov from prov");
while(rs.next())
{
CBProvedores.addItem(rs.getString("name_prov"));
//Here should be the Value related to the item I am creating
}
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "Error" + e );
}
有什么办法能让我做到吗?
2条答案
按热度按时间4ioopgfo1#
首先创建一个POJO,它将同时包含
name
和id
。然后使用此POJO对象将其放入
JComboBox
。最后,要选择值,您可以这样做:-
c90pui9n2#
你好,我也是java和javafx的新手。这是我在javafx中做的,它对我很有效,希望你能在java中解决它。
希望它对你有用。注意我的我的组合框名称是bankName