0

I want to populate jTable from database I used a code that a member on stackoverflow proposed for me here is the code

UsersTableModel.java:

import com.home.user.db.vo.UsersVo;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

            public class DefaultTableModel extends AbstractTableModel{
            private String[] columnNames =
            {
                "FIRST NAME",
                "LAST NAME",
                "AGE" 
            }
         
            private List<UsersVo> users;
         
            public DefaultTableModel()
            {
                users = new ArrayList<UsersVo>();
            }
         
            public DefaultTableModel(List<UsersVo> users)
            {
                this.users = users;
            }
        
            @Override
            public int getColumnCount()
            {
                return columnNames.length;
            }
         
            @Override
            public String getColumnName(int column)
            {
                return columnNames[column];
            }
         
            @Override
            public int getRowCount()
            {
                return users.size();
            }
            
            @Override
            public Class getColumnClass(int column){
            switch (column)
            {
                case 2: return Date.class;
                default: return String.class;
            }
        }
        @Override
            public boolean isCellEditable(int row, int column){
            switch (column)
            {
                case 2: return true; // only the birth date is editable
                default: return false;
            }
        }
            @Override
            public Object getValueAt(int row, int column){
            UsersVo users = getUserVo(row);
         
            switch (column)
            {
                case 0: return users.getFirstName();
                case 1: return users.getLastName();
                case 2: return users.getAge();
           ;
                
                default: return null;
            }
        }
            @Override
            public void setValueAt(Object value, int row, int column){
            UsersVo users = getUserVo(row);
            switch (column)
            {
                case 0: users.setFirstName((String)value); break;
                case 1: users.setLastName((String)value); break;
                case 2: users.setAge((Date)value); break;
            }
            fireTableCellUpdated(row, column);
        }
            
            public UsersVo getUserVo(int row){
            return users.get( row );
           }
        
            public void addUserVo(UsersVo user){
            insertUsersVo(getRowCount(), user);
            }
     
            public void insertUsersVo(int row, UsersVo user){
            users.add(row, user);
            fireTableRowsInserted(row, row);
           }
    }

this is the usersVo.java :

public class UsersVo {

private String firstName;
private String lastName;
private Date age;

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public Date getAge() {
    return birthdate;
}

public void setAge(Date age) {
    this.birthdate = birthdate;
}

UsersDao.java: (Implementation the abstract method from DaoList)

public class UsersDao extends Dao implements DaoList<UsersVo>{
    
    private static UsersDao userDao;
    private Object users;
    
    private UsersDao(){
    
    }
    
    public static UsersDao getInstance(){
    
        if(userDao == null){
            userDao = new usersDao();
        }
        return usersDao;
    }
     private Object usersModel;
        
        @Override
        public UsersVo getData(UsersVo uv) throws Exception {
        Connection con = null;
        UsersVo userVo = 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()){
                userVo = new UsersVo();
                userVo.setFirstName(rs.getString("FIRST_NAME"));
                userVo.setLastName(rs.getString("LAST_NAME"));
                userVo.setBirthDate(rs.getDate("BIRTH_DATE"));
                usersModel.addUserVo(userVo);
            }
        
        }catch(Exception ex){
             JOptionPane.showMessageDialog(null,ex.getMessage());
        }finally{
                rs.close();
                ps.close();
                closeConnection(con);
        }
        return userVo;
    }

DaoList interface :

public interface DaoList<T> {

    public List<T>
    loadAll() throws Exception;
    public int insert(T t) throws Exception;
    public int update(T t) throws Exception;
    public int delete(T t) throws Exception;
    public T getData(T t) throws Exception;
    public T getDataById(int id) throws Exception;
    
}

UserView.java:

public class UsersView extends javax.swing.JFrame {

UsersTableModel utm = new UsersTableModel();

public UsersView() {
    initComponents();
    this.setLocationRelativeTo(null);
    jTable1.setModel(utm);
  
}
    .
    .
    .

I only get the column names in the table without data from database can you tell me where is the problem please ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Why are you so confused??? Did you not download and execute the code from: https://stackoverflow.com/a/55635012/131872. The code is contained in a single class. So you don't have to worry about passing variables and you don't have to worry about creating custom classes. First learn how to add data to the DefaultTableModel using a Vector. Then worry about using your custom model and user object once you understand the basics. Learn to walk before you run. – camickr Jan 11 '21 at 00:26
  • Yes. "utm" is the TableModel. You you need to add data to the TableModel. This is basic Java. You create an instance of an object and then you invokes methods of that object. So your `getData()` method needs access to the "utm" variable so you can add data to the model. – camickr Jan 11 '21 at 00:27
  • bro I learned basic by myself even I'm not good in english I learned english by myself as well haha so this is why I find difficult to understand the basics. well I have already created an instance of UsersVoTableModel in the UsersView.java file to be honest I dont know how to invoke that to the method getData() do I need to do somthing like UsersVo u = UsersDB.getInstance().getData(); ? ... I'm lost –  Jan 11 '21 at 01:32
  • 1
    *I'm lost –* - which is why I have told you several times to forget this approach and use the code from the link provided in the first comment above. It appears you don't understand Java basics about passing parameters from one method to another. The code you posted is not complete, so I have no idea how your code is structured, so I can't make a suggestion on how to pass the model to you getData() method. That is why you should use the code in the link. All the code is contained in a single class. Nothing to change except the connection to your database. – camickr Jan 11 '21 at 02:17
  • I updated the code I have included all the files except the Dao.java that makes the connection to tadabase there is no issue with the connection to database. I have not included the methods (insert, delete, update) in UsersDao.java to shorten the code –  Jan 11 '21 at 02:47
  • 1
    Well, the fact that you are using DAO is a rather important piece of information to leave out. I have never used DAO but a quick search of the internet and I found this basic tutorial: https://javatutorial.net/what-is-dao-and-how-to-use-it to give me some basics. So, using your DaoList I would say the 3 relevant steps are: 1) invoke the `loadAll()` method to get a List of all UserVo objects. 2) use this List to create an instance of your `UserVoTableModel`. 3) add this instance of the table model to your JTable. If that doesn't help then I'm out of ideas. – camickr Jan 11 '21 at 03:24
  • @camickr whatever I have learned from your few things thank you for your time –  Jan 11 '21 at 04:02

0 Answers0