2012-10-13 51 views
0

我有一個文本框,它必須從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。如果我搜索其他記錄仍然在表中。我想如果我搜索一個特定的記錄。搜索記錄將顯示在表格中。

+0

如果您不介意使用第三方lib:SwingX項目內置了「JXTable」的搜索功能, – Robin

回答

0

您對內部模型執行篩選,並使用篩選結果更新表模型。

1
  • 具有看看JTable tutorial

  • about Sorting and Filtering,例如在本教程

  • 爲更好地幫助越早發佈一個SSCC E,因爲你的代碼演示JDBC和PreparedStatement,

  • 你將有Concurency in Swing的問題,Swing GUI將不負責任(鼠標和鍵盤事件),直到長時間運行JDBC Statement結束,(在這種形式下,你的代碼),

  • 具有重定向JDBC Statement to the BackGround Task, use的Runnable#主題or SwingWorker`

  • 所有更新的Swing GUI必須Event Dispatch Thread

0

完成你應該在你的項目中添加這個rs2xml.jar的jar /文件夾

String sql =「Select * From Inventarizimi where Regjistrimi =?」;
Class.forName(「oracle.jdbc.driver.OracleDriver」);連接con =(連接)DriverManager.getConnection(「jdbc:oracle:thin:@localhost:1521:XE」,「Inventarizimi」); PreparedStatement preStatement = con.prepareStatement(sql); prestatement.setString(1,txtRegjistrimi.getText()); ResultSet result = preStatement.executeQuery();
table.setModel(DbUtils.resultSetToTableModel(result));

相關問題