Aldrin Berana
Aldrin Berana

Reputation: 1

How can I search particular data in Jtable?

I have a textfield and it must search a particular records from the Jtable. How can I search particular records in the table? The records that had been search will only be display at the table. after searching it can be highlight and put the value of the highlighted records in a txtfield or label.

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);    
}                                     
}

It only search for the Customer ID. and if I search other records are still in the table. I want if I search for a particular record. The search record will be displayed at the table.

Upvotes: 0

Views: 2318

Answers (3)

Rrezart A. Prebreza
Rrezart A. Prebreza

Reputation: 33

You should to add jar/folder this rs2xml.jar in your project

String sql="Select * From Inventarizimi where Regjistrimi=?";
Class.forName("oracle.jdbc.driver.OracleDriver"); Connection con= (Connection) 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));

Upvotes: 0

mKorbel
mKorbel

Reputation: 109815

  • have look at JTable tutorial

  • about Sorting and Filtering, example in the tutorial

  • for better help sooner post an SSCCE, because your code demonstrating JDBC and PreparedStatement,

  • you'll have issue with Concurency in Swing, Swing GUI will be unresponsible (mouse and key events) until hard and long running JDBC Statement ended, (in this form, your code),

  • have to redirect the JDBC Statement to the BackGround Task, useRunnable#ThreadorSwingWorker`

  • all updates to the Swing GUI must be done on Event Dispatch Thread

Upvotes: 1

Dan D.
Dan D.

Reputation: 32391

You do the filtering on your internal model and you update the table model with the filtered results.

Upvotes: 0

Related Questions