我有一個文本框,它必須從Jtable中搜索特定的記錄。我如何搜索表格中的特定記錄?搜索過的記錄只會顯示在表格中。搜索後可以突出顯示並將突出顯示的記錄的值放在txtfield或標籤中。如何在Jtable中搜索特定數據?
private void txtsearchKeyReleased(java.awt.event.KeyEvent evt) {
try{
String sql = "select * from customer where Customer_ID=?";
pst=conn.prepareStatement(sql);
pst.setString(1, txtsearch.getText());
rs= pst.executeQuery();
if(rs.next()){
String add1 =rs.getString("Customer_ID");
lblID.setText(add1);
String add2 = rs.getString("First_Name");
lblfname.setText(add2);
String add3 = rs.getString("Last_Name");
lbllname.setText(add3);
String add4 = rs.getString("Birthdate");
lblbirthdate.setText(add4);
String add5 = rs.getString("Gender");
lblgender.setText(add5);
String add6 = rs.getString("Occupation");
lbloccupation.setText(add6);
String add7 = rs.getString("Address");
lbladdress.setText(add7);
String add8 = rs.getString("Email");
lblemailadd.setText(add8);
String add9 = rs.getString("Contact");
lblcontact.setText(add9);
String add10 = rs.getString("Status");
lblstatus.setText(add10);
String add11 = rs.getString("Income");
lblincome.setText(add11);
String add12 = rs.getString("Amount");
lblamount.setText(add12);
String add13 = rs.getString("Months");
lblterm.setText(add13 +" months");
String add14 = rs.getString("Interest");
lblinterest.setText(add14);
String add15 = rs.getString("Date_Applied");
lblapplied.setText(add15);
String add16 = rs.getString("Purpose");
lblpurpose.setText(add16);
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
它只搜索客戶ID。如果我搜索其他記錄仍然在表中。我想如果我搜索一個特定的記錄。搜索記錄將顯示在表格中。
如果您不介意使用第三方lib:SwingX項目內置了「JXTable」的搜索功能, – Robin