所以我的框架上有这个 jTable 和一个 jComboBox。我的 jComboBox 里面是电话列表。我想要做的是让 jTable 获取 jComboBox 内特定产品的数据库。假设我在 jComboBox 中选择 Samsung S7。当我单击“详细信息”jButton 时,jTable 将显示三星 S7 的数据(型号、价格、库存等)。我该怎么做呢?这是我的代码:
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/projectephone","root","");
String sql= "select * from samsung";
PreparedStatement pst = con.prepareStatement(sql);
ResultSet rs= pst.executeQuery();
jTable1.setModel(DbUtils.resultSetToTableModel(rs));
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Failed to add");
}
}
但是这段代码只是让我的 jTable 显示数据库表中的所有数据。有什么想法吗?
最佳答案
单击详细信息按钮时
从
JComboBox
获取当前选定的手机(以手机名称或数据库表中的内部 ID 或其他形式给出)然后使用该值来限制您的搜索。
例如
String phoneName = ... // current selected in the the combobox
String sql= "select * from samsung where name = ?";
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, phoneName);
ResultSet rs= pst.executeQuery();
关于java - 需要有关 jComboBox 和 jTable 的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35969132/