0

I have problem with my list. Adds a list to the class. In the class list more items are added. When you want to display a list of the class do not have any items.

Add people to MySQLCategoryDAO -> modify in MySQLCategoryDAO -> return people in class Model.

public class Model {

    public List<Category> people = new Vector<Category>();

    public List<Category> getPeople() {

        for (Category person : people) {
            System.out.println(person.getName());  //no data
        }
        return new ArrayList<Category>(people);
    }

    public void load() throws Exception {

        DAOFactory factory = DAOFactory.getFactory(DAOFactory.MYSQL);

        new MySQLCategoryDAO(Job.SELECT,people).execute();
    }

}

Class in which modifies the list

public class MySQLCategoryDAO extends SwingWorker<Void, Category> implements CategoryDAO{

    private Job job;
    public List<Category> list;


    public MySQLCategoryDAO(Job job, List<Category> list){
        this.job = job;
        this.list = list;
    }
    @Override
    public  Void doInBackground() throws Exception {
        // TODO Auto-generated method stub

        if(job == Job.SELECT){
            getCategory();
            System.out.println("Table selected");
        }       
        return null;
    }   
    public List<Category> getCategory() throws SQLException
    {
        Connection conn = Database.getInstance().getConnection();

        System.out.println(conn);

        String sql = "select id, name from kategorie";
        Statement selectStatement =  conn.createStatement();
        ResultSet results = selectStatement.executeQuery(sql);

        while(results.next())
        {
            int id = results.getInt("id");
            String name = results.getString("name");

            ///Category category = new Category(id, name);
            //cat.add(category);
            publish(new Category(id,name));
            System.out.println("test");
        }
        results.close();
        selectStatement.close();

        return null;

    }
    public  void process(List<Category> chunks){
        list.addAll(chunks);

        for (Category person : list) {
            System.out.println(person.getName()); //data SHOW OK!
        }
    }
}

When you want to refer to the list of model did not get.

public void loadData() {

        MySQLCategoryDAO dao = new MySQLCategoryDAO(null,null);
        people = model.getPeople();

        for (Category person : people) {
            tablemodel
                    .addRow(new Object[] { person.getId(), person.getName() }); //no data
        }

        people.clear();

    }
lukassz
  • 127
  • 1
  • 2
  • 12

0 Answers0