TransWikia.com

Data doesn't display in jTable

Stack Overflow Asked by user14841908 on February 5, 2021

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 ?

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP