gpt4 book ai didi

java - 如何搜索 SQL 数据库并将其显示在 JTable 上

转载 作者:行者123 更新时间:2023-11-29 08:08:30 26 4
gpt4 key购买 nike

就像标题所说的那样,我想搜索 SQL 数据库并使用 keyReleased 上的 jTextField 将其显示在 JTable 上。这是我尝试过的:

 private void jTextField2KeyReleased(java.awt.event.KeyEvent evt) {                                        

try{
int get = (int)jTable2.getModel().getValueAt(jTable2.getSelectedRow(), 0);
String query = "SELECT FName FROM roominfo WHERE FName LIKE '%" + jTextField2.getText() + "%'";

String url = "jdbc:mysql://localhost:3306/adv";
Connection conn = DriverManager.getConnection(url,"root","sa");
Statement st = conn.createStatement();
rs = st.executeQuery(query);
jTable2.setModel(DbUtils.resultSetToTableModel(rs));

conn.close();

}
catch (Exception e) {
System.err.println("Got an exception! ");
JOptionPane.showMessageDialog(null,"Got an exception!");
System.err.println(e.getMessage());
} // TODO add your handling code here:
}

但是,我不能正确地做到这一点。似乎有什么问题?它仍然不搜索。

最佳答案

按下按键时:

try{
String url = "jdbc:mysql://localhost:3306/adv";
Connection conn = DriverManager.getConnection(url,"root","sa");
Statement st = conn.createStatement();
String query = "SELECT CustomerNo, FName, FROM roominfo WHERE FName LIKE '%" + jTextField2.getText() + "%'";
rs = st.executeQuery(query);
jTable2.setModel(DbUtils.resultSetToTableModel(rs));

conn.close();

}
catch (Exception e) {
JOptionPane.showMessageDialog(null,"Got an exception!");
System.err.println(e.getMessage());
}

关于java - 如何搜索 SQL 数据库并将其显示在 JTable 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22219504/

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