在我的程序中,我可以使用程序中的 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){}
此外,根据我上传的图片,我的问题是:如何通过姓名
、中间名
和姓氏
搜索学生使用组合框?
例如,我在组合框中选择 MiddleName
,然后当我在 jTextField“txtsearch”中键入时,我只能在表的 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();
关于java - 如何使用 MS Access 使用 ComboBox 和 jTextField Netbeans 搜索 jTable 中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33548096/