gpt4 book ai didi

java - SQL : WHERE not working with JTable

转载 作者:行者123 更新时间:2023-12-01 10:15:33 24 4
gpt4 key购买 nike

我正在使用 valueChanged() 将选定的 JTree 节点设置为 JTable,但我注意到使用 WHERE 时没有得到任何结果。删除 WHERE 后得到完整结果。

 @Override
public void valueChanged(TreeSelectionEvent tsl) {
try {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) TestTree.getSelectionPath().getLastPathComponent();
myTests_table.getModel();

if (tsl.getNewLeadSelectionPath() != null) {
if (node.isLeaf() != true) {
//do nothing
} else {
String sql ="SELECT sub_group.name AS 'name', sub_group.price AS 'price'"
+ "FROM sub_group"
+ "WHERE sub_group.name = '"+node+"' "
+ "ORDER BY sub_group.name ASC";

pst = conn.prepareStatement(sql);
rs = pst.executeQuery(sql);
while (rs.next()) {
myTests_table.setModel(DbUtils.resultSetToTableModel(rs));
}

System.out.println(node);
JOptionPane.showMessageDialog(null, node);
yy.setText(tsl.getNewLeadSelectionPath().getLastPathComponent().toString());

}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Error");
} finally {
try {
rs.close();
pst.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Some Error with connecting Database, Try again");
}
}
}

但是当我使用这条线时

+ "WHERE sub_group.name = '"+node+"' "

我的表是空的,那么有什么问题吗?!

最佳答案

代码不足以检查。更新总代码或执行 sysout 并更新结果,可能会看到错误是什么

 String sql ="SELECT sub_group.name AS 'name', sub_group.price AS 'price'"
+ "FROM sub_group"
+ "WHERE sub_group.name = '"+node+"' "
+ "ORDER BY sub_group.name ASC";
System.out.println(sql);

关于java - SQL : WHERE not working with JTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35920859/

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