1

I have a database table which has many patient details like registration id, registration date etc. I want to perform a date based search of the patients and get the result into a jTable. ( for example, if I type 23-05-2013 as the registration date, all the patients admitted on 23 May should be displayed in a jTable. How do I do that?

This is the code I have used:

public void getTableData() {
  try {
     con = getConnection.getCon();
     String sql = "SELECT * FROM patientrecords WHERE Registration Date = ?";
     pst.setString(1, regdate.getText());
     pst = con.prepareStatement(sql);
     rs = pst.executeQuery();
     if (rs.next()) {
        patient_table.setModel(DbUtils.resultSetToTableModel(rs));
     }
  } catch (SQLException e) {
     JOptionPane.showMessageDialog(null, e);
  }
}
nachokk
  • 14,363
  • 4
  • 24
  • 53
user2636662
  • 63
  • 1
  • 7
  • 1
    I edited your post to fix the all caps in the title. Please don't do that. Also, please slow down and take your time writing your question. When it's neat and readable, people will take you more seriously. Welcome to Stack Overflow! :) – jamesmortensen Jul 31 '13 at 05:33
  • Start by taking a look at [How to use tables](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html) – MadProgrammer Jul 31 '13 at 05:42
  • @MadProgrammer now im thinking that his code may be works xD – nachokk Jul 31 '13 at 05:51

1 Answers1

2

1) You should make your own TableModel implementation or extending AbstractTableModel

2) Map patientrecords to object world. Also entity name should be in singular. So you will have a java-bean like PatientRecord.

3) You'll have a List<PatientRecord> data as DataHolder.

Fill like

while (rs.next()) {
        data.add(new PatientRecord(..));
}

4) In some part you should set in your jtable.setModel(..)

This previous question may help you simple code to populate jtable from resultset

Community
  • 1
  • 1
nachokk
  • 14,363
  • 4
  • 24
  • 53