skip to Main Content

I am using phpMyAdmin through xampp as my host, I can add data just fine but when I run the application it doesn’t show any data. Could someone please check my code. I am new to programming in java so please bear with me.

public DefaultTableModel getmodel(){
DefaultTableModel model = (DefaultTableModel) jtbl.getModel();
                model.setRowCount(0);
                show_data_in_jtable();
                return null;
}
public ArrayList<User> getuserList()
  {
       ArrayList<User> userList =  new ArrayList<User>();
        Connection Conn2 = m2.getConnection();
        String query = "select * from Student ";
        Statement st;
          ResultSet rs;

          try{
          st = Conn2.createStatement();
          rs = st.executeQuery(query);
          User user_var;

          while(rs.next())
                {
            user_var = new User(rs.getInt("id"),rs.getString("fname"),rs.getString("lname"),rs.getString("mname"),rs.getInt("age"),rs.getString("Gender"),rs.getString("course"));
            userList.add(user_var);
                }
          }
          catch (Exception error)
          {
              error.printStackTrace();
            }
          return userList;
    }
// display data on Jtable
public void show_data_in_jtable()
   {
   ArrayList<User> list = getuserList();
   DefaultTableModel model = (DefaultTableModel)  jtbl.getModel();
    Object[] column = new Object[5];

    for(int i = 0; i < list.size(); i++)
            {
               column  [0] = list.get(i).getId();
                   column [1] = list.get(i).getFirstName();
                      column [2] = list.get(i).getLastName();
                      column [3] = list.get(i).getLastName();
                         column [4] = list.get(i).getMiddleName();
                            column [5] = list.get(i).getGender();
                            column [6] = list.get(i).getCourse();
                         model.addRow(column);
            }
    }

2

Answers


  1. Did you set your model to a Jtable?

    If you did that what is result of getuserList() function? Did you debug your code?

    Check this How to Use Tables

    Login or Signup to reply.
  2. This isn’t really an answer to your question, as I think your problem stems from not adding the model correcting to the table, however I would encourage you to not use the DefaultTableModel. Create your own model by extending AbstractTableModel, it will make your life easier in the long run:

     public class UserTableModel extends AbstractTableModel {
    private List<User> users = new ArrayList<>();
    @Override
    public int getColumnCount() {
        return 6;
    }
    
    public String getColumnName(int col) {
        switch (col) {
        case 0:
            return "ID";
        case 1:
            return "Last Name";
        case 2:
            return "First Name";
        case 3:
            return "Middle Name";
        case 4:
            return "Gender";
        case 5:
            return "Course";
        }
        return null;
    }
    @Override
    public int getRowCount() {
        return users.size();
    }
    
    public void setUsers(List<User> users) {
        this.users.clear();
        if (users != null) {
            this.users.addAll(users);
        }
        fireTableDataChanged();
    }
    
    public void addUser(User user) {
        users.add(user);
        fireTableRowsInserted(users.size() - 1, users.size() - 1);
    }
    
    public void removeUser(User user) {
        if (user != null) {
            users.removeIf(u -> u.getId().contentEquals(user.getId()));
        }
        fireTableDataChanged();
    }
    public void removeRow(int row) {
        if (row >= 0 && row < users.size()) {
            users.remove(row);
            fireTableDataChanged();
        }
    }
    @Override
    public Object getValueAt(int row, int col) {
        if (row >= 0 && row < users.size() && col >= 0 && col < getColumnCount()) {
            User user = users.get(row);
            switch (col) {
            case 0:
                return user.getId();
            case 1:
                return user.getFirstName();
            case 2:
                return user.getLastName();
            case 3:
                return user.getMiddleName();
            case 4:
                return user.getGender();
            case 5:
                return user.getCourse();
            }
        }
        return null;
    }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search