java - 从数据库中获取组合框值
问题描述
我想ComboBox
从数据库中获取值。我正在尝试以下代码,它不会返回任何错误,但不会返回添加到 ComboBox 的项目。
@FXML
private ComboBox<List> laptopos_combo;
public void filloscombo() throws SQLException
{
String connectionUrl = "jdbc:sqlserver://WIN\\SQLEXPRESS:1433;database=itinventory;" +
"user=sa;"+ "password=Varpal@1234;";
Connection connect= DriverManager.getConnection(connectionUrl);
String query = "Select * from operatingsystem";
PreparedStatement pst = connect.prepareStatement(query);
// log.conn().prepareStatement(query);
ResultSet os = pst.executeQuery();
while(os.next())
{
((List<String>) laptopos_combo).add(os.getString("os"));
}
}
解决方案
我认为您需要将 ComboBox 的项目类型从 List 更改为 String,因为您想向 ComboBox 添加一个字符串值,请尝试以下操作:
@FXML
private ComboBox<String> laptopos_combo;
public void filloscombo() throws SQLException {
String connectionUrl ="jdbc:sqlserver://WIN\\SQLEXPRESS:1433;database=itinventory;" +
"user=sa;"+ "password=Varpal@1234;";
Connection connect= DriverManager.getConnection(connectionUrl);
String query="Select * from operatingsystem";
PreparedStatement pst = connect.prepareStatement(query);
ResultSet os = pst.executeQuery();
laptopos_combo.getItems().clear(); // Add this line to remove all items before you add the database one
while(os.next()) {
laptopos_combo.getItems().add(os.getString("os"));
}
}
推荐阅读
- python - 如何使用 Selenium Python 在#shadow-root(打开)中提取信息?
- c# - 如何在 c# 中设置 windows 窗体以接受文本框和组合框中的空值
- discord.js - 在 discord.js 中创建具有多个参数的命令
- java - 如果我没有模拟器,我如何运行我的 WEAR OS 代码
- python-3.x - 如何对数据框进行排序?
- c# - 简单的 Sql 角色查询问题
- python - 如何在 pyqt 简单 gui 窗口旁边绘制 matplotlib 图?
- git - Jenkins Pipeline Git Push 通过 Windows 代理节点
- javascript - 为什么用 Jest 测试 Vue 组件时 document.activeElement 不指向 JSDom 中的焦点元素?
- java - com.microsoft.sqlserver.jdbc.SQLServerException:无效的对象名称“banco_embozado_especial”