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();

    }
3
  • 1
    You are not calling the process method anywhere.. is that intentional? Commented Aug 17, 2014 at 19:41
  • Method process just adds data to the list. He wants to get to the list of other classes. Commented Aug 17, 2014 at 19:46
  • Ok. Solution for this problem: stackoverflow.com/questions/25339134/java-thread-for-jdbc in comments. Commented Aug 18, 2014 at 22:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.