0

Hello I want to populate a table from database but I have some difficult to do since I am newbie in java programming here is the Users.java where the method getData(select) is implemented:

@Override
public Users getData(Users u) throws Exception {
    Connection con = null;
    Users user = null;
    ResultSet rs = null;
    PreparedStatement ps = null;
    try{
        con = getConnection();
        String sql = "SELECT * FROM USERS";
        ps = con.prepareStatement(sql);
        rs = ps.executeQuery();
        while(rs.next()){
            user = new Users();
            user.setFirstName(rs.getString("First_NAME"));
            user.setLastName(rs.getString("LAST_NAME"));
            user.setAge(rs.getInt("AGE"));  
        }
    
    }catch(Exception ex){
         JOptionPane.showMessageDialog(null,ex.getMessage());
    }finally{
            rs.close();
            ps.close();
            closeConnection(con);
    }
    return user;
}

Well the data is stored in Users object now and I want to display it on a jTable which is located in patientJframe file can you tell me how to do that? I created a method on patientJframe but I dont know what to do Im stuck onhere.

PatientJframe :

    public void PopulatejTable(){

   try { 
   
       Users u = null;
       Users user = UsersDB.getInstance().getData(u);
       
       if(user== null){
       DefaultTableModel dtm = (DefaultTableModel)jTable.getModel();
       dtm.setRowCount(0);
       Vector vector = new Vector();
       vector.add(user.getCin());
       vector.add(user.getLastName());
       vector.add(user.getFirstName());
       }else{
       JOptionPane.showMessageDialog(null,"Faild");
       }
       } catch (Exception ex) {
      Logger.getLogger(AddNewPatient.class.getName()).log(Level.SEVERE, null, ex);
    }
}

The method it is correct ? please can you help me ?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Well the code doesn't really do anything. You keep creating User objects but then don't do anything with them. If you have multiple Users then you need to add each User to the TableModel as you read the data from the database. See: https://stackoverflow.com/a/55635012/131872 for a generic example. If you want to add User objects to the TableModel, then you need to create a custom TableModel. See: [Row Table Model](https://tips4java.wordpress.com/2008/11/21/row-table-model/) for a step-by-step example on how to do this. – camickr Jan 08 '21 at 16:51
  • Generally, you would read the User table rows into a java.util.List, and use the List to populate a TableModel. – Gilbert Le Blanc Jan 08 '21 at 17:28
  • Hi @camickr that what I'm looking for thank you for help –  Jan 08 '21 at 18:16
  • @Gilbert Le Blanc how to do ? –  Jan 08 '21 at 18:18
  • [How to fill data in a JTable with database?](https://stackoverflow.com/questions/2192764/how-to-fill-data-in-a-jtable-with-database). Take a look at the top answer. – Gilbert Le Blanc Jan 08 '21 at 20:28
  • Hi @camickr I created a custom TableModel as you proposed for me but the data not displaying in jTable except the column names here is the code https://stackoverflow.com/questions/65645493/data-doesnt-display-in-jtable –  Jan 09 '21 at 18:29

0 Answers0