1.11M Members

jTable Database

 
0
 

Ok so i have a little problem which i cant seem to figure out
this is what i have:

The constructor loads my method that loads a list of employee to show in jTable, but when i place that method in my button it doesn't do anything

This is my code:

Method that loads the employees from my DBEngine Class

public void viewEmpleados(){
        try {
            DBEngine dbe = new DBEngine();
            dbe.getEmployee();
            data = dbe.getEmployee();
            header = new Vector<String>();
            
            header.add("Id");
            header.add("Fname");
            header.add("Lname");
            header.add("Departamento");
            header.add("Puesto");
            
        } catch (Exception ex) {
        }
        
    }

This is the DBEngine.getEmployee method

public Vector getEmployee()throws Exception{
            Vector<Vector<String>> employeeList = 
                        new Vector<Vector<String>>();
            dbConnection(); //My db connection           
                String empQuery = "Select idEmpleado,fname,lname,dep From Empleados;";
                rs = st.executeQuery(empQuery);

                while(rs.next()){
                    Vector<String> employee = new Vector<String>();
                    employee.add(rs.getString(1));
                    employee.add(rs.getString(2));
                    employee.add(rs.getString(3));
                    employee.add(rs.getString(4));
                    employeeList.add(employee);
                }
                con.close();
            return employeeList;
    }

I want my jTable to refresh each time i press the button "View Employee" after i add an employee from a form, or when i add it directly from Postgresql

Thanks if anyone can help
:/
:)

 
0
 

You can make an update function that updates your jtable everytime you add/remove employee. Call this function after you add or remove an item in your table.

it might look something like this.

public void updateTable(JTable table){
    DefaultTableModel model = (DefaultTableModel)table.getModel();
    List<Employee> employees = employeeDao.getEmployees();
    model.setRowCount(0); //empty your table
    for(Employee employee:employees){
        //add old and new data to table
        model.addRow(new Object[]{employee.getFirstName(),employee.getLasName(),
            employee.getDepartment()});
    }
}

public void addEmployee(Employee employee){
    //insert employee to your database
    employeeDao.addEmployee(employee);
    //update your table to include the inserted employee in table
    updateTable(employeeJTable);
}
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: