java - 如何在组合框中显示值并将其 id 保存到数据库中
问题描述
我有一个显示值而不是 Id 的组合框。然后应该将 id 插入数据库。我向组合框添加了一个侦听器以获取 ID。通过 sysout 我可以看到,但我怎么能插入数据库????
这是代码:
public void comboboxpersonneldata() {
comboboxpersonnellist = FXCollections.observableArrayList();
handler = new DBHandler();
connection = handler.getConnection();
try {
rs = connection.createStatement().executeQuery("SELECT num_personnel,nom_personnel FROM personnel");
while (rs.next()) {
// comboboxpersonnellist.add(rs.getInt(1));
comboboxpersonnellist.addAll(new comboboxPersonnel(rs.getInt(1), rs.getString(2)));
}
combopersonnel.setItems(comboboxpersonnellist);
combopersonnel.valueProperty().addListener((obs, oldval, newval) -> {
if (newval != null) {
System.out.println(newval.getNum_personnel());
}
});
} catch (SQLException e) {
e.printStackTrace();
}
try {
rs.close();
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
和模型类:
public class comboboxPersonnel {
int num_personnel;
String name_personnel;
public comboboxPersonnel(int num_personnel, String name_personnel) {
this.num_personnel = num_personnel;
this.name_personnel = name_personnel;
}
public int getNum_personnel() {
return num_personnel;
}
public String getName_personnel() {
return name_personnel;
}
@Override
public String toString() {
return name_personnel;
}
}
解决方案
推荐阅读
- typescript - 当 Promise 返回类型未等待时在 VS Code 中发出警告?
- jquery - 使用引导表扩展行后初始化元素
- azure-ad-b2c - Azure B2C 修改 SAML AccessTokenLifetime
- node.js - 即使在我覆盖 res.status 之后,NodeJS Azure 函数也会返回状态 500
- r - 用所有因素组合的观测值汇总数据
- javascript - 处理 Electron 上的打印事件
- r - 如何将光栅 ASCII 文件转换为 R 中经纬度的数据帧?
- python - 在带有文件的终端中运行python shell?
- css - CSS 属性 z-index 不适用于汉堡菜单
- php - MongoDB Geospatial,项目匹配多点