Reputation: 139
i'm making simple lab management system using java swing GUI where i need to print out all patients data from database table but i cannot found how to retrieve all rows data from database. With below code i'm getting only last row data.
private void inner_search_btnActionPerformed(java.awt.event.ActionEvent evt) {
String search = this.search_field.getText();
String name = null;
try{
if(search.trim().length() == 0){
this.results_field.setText("Please fill in search field !!!");
}
else
{
String query = "select * from lab_tests";
rs = st.executeQuery(query);
int i =0;
while(rs.next())
{
i++;
name = rs.getString("patient_name");
this.results_field.setText("<html>" + name + "<br></html>");
}
}
}
catch(Exception ex){
}
}
Upvotes: 0
Views: 3587
Reputation: 667
best would be to make a domain class, something like
class Patient {
private String fName;
private String lName;
private int age;
public void setfName(String s){fName=s;}
public String getfName(){return fName;}
public void setlName(String s){lName=s;}
public String getlName(){return lName;}
public void setAge(Int i){age=i;}
public int getAger(){return age;}
}
and in the function where you are getting data from the data base be like:
public ArrayList<Patient> getPatients(){
public ArrayList<Patient> pList= new ArrayList<Patient>();
// part where you do you query on DB
while(rs.next())
{
Patient p = new Patient();
p.setfName(rs.getString("patient_name"));
p.setlName(rs.getString(2));
p.setAge(rs.getInt(3));
pList.add(p);
}
return pList;
}
and then add that data to Jtable
Upvotes: 0
Reputation: 9872
you are replacing text each time .that's why you are getting only final database record .use jtextarea
instead jtextfield
and use append method
textArea.append(name + "\n");
or you can use stringBuilder
Upvotes: 2