0

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){

    }
}   
3
  • setText("<html>" + name + "<br></html>"); replace text Commented Dec 17, 2014 at 23:03
  • 2
    Beware of fetching all rows from the database. Your UI will slow to a crawl if your table has too many rows (think thousands of records). Commented Dec 17, 2014 at 23:09
  • 1
    @Brian Only if you've done something wrong... Commented Dec 17, 2014 at 23:42

2 Answers 2

2

you are replacing text each time .that's why you are getting only final database record .use jtextareainstead jtextfield and use append method

textArea.append(name + "\n");

or you can use stringBuilder

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help, is there any better way to display data instead of textarea ?
@anasnaqvi yes you need a jtable .it's the best component for that
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.