如何使用带有 jTextField Netbeans 的 ComboBox 使用 MS Access 在 jTable 中搜索数据

How to search Data in jTable using MS Acess using ComboBox with jTextField Netbeans

在我的程序中,我可以使用此代码在我的程序中使用 jTextfield "txtsearch" 在 jTable 中搜索数据。

请看我的实际程序。这是一张图片:

这是我在 jTextField 中的代码 "txtsearch"。在这段代码中 我只能通过 Name 搜索 jTable。我想使用 jComboBox 进行搜索。

String a=txtsearch.getText();
conn=MyConnection.ConnectDB();
String sql="Select* from StdRecord WHERE Name LIKE'"+a+"%'";
try{
    pst=conn.prepareStatement(sql);
    rst=pst.executeQuery();
    jTable1.setModel(DbUtils.resultSetToTableModel(rst));
}catch(Exception e){}

此外,根据我上传的图片,我的问题是:如何使用组合框按 NameMiddleNameSurname 搜索学生? 例如,我在组合框中 select MiddleName,然后当我在 jTextField 中键入 "txtsearch" 时,我只能在 table 的 MiddleName 部分中搜索。

This is my code in jTextField "txtsearch". In this code I can search the jTable via Name only. I want to search by using the jComboBox.

您当前的 SQL 对要搜索的数据使用变量,那么为什么不能对要搜索的列使用变量?

String a=txtsearch.getText();
String column = comboBox.getSelectedItem().toString();
String sql="Select* from StdRecord WHERE " + column + "LIKE'"+a+"%'";

此外,为了使 SQL 更易于编码和维护,您应该使用 PreparedStatement:

String sql = "Select* from StdRecord WHERE " + column + "LIKE ?";
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString( 1, a + "%" );
stmt.executeQuery();