Java SwingWorker将数据从数据库加载到List

时间:2014-08-17 15:58:47

标签: java swing

我的MVC应用程序出现问题,它在JTable中显示数据。一切正常,但我决定添加一个SwingWorker来从数据库中检索数据。

我的控制器使用数据库中的数据调用模型。看起来像这样。

Model.java

    public class Model {

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

    public List<Category> getPeople() {
        return new ArrayList<Category>(people);
    }

    public void load() throws Exception {
        people.clear();
        DAOFactory factory = DAOFactory.getFactory(DAOFactory.MYSQL);
        CategoryDAO personDAO = factory.getCategoryDAO();
        people.addAll(personDAO.getCategory());
    }

}

我将SwingWorker添加到getCategory类

MySQLCategodyDAO.java

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

    private Job job;
    private List<Category> cat;

    public MySQLCategoryDAO(Job job){
        this.job = job;
    }

    @Override
    protected Void doInBackground() throws Exception {
        // TODO Auto-generated method stub

        if(job == Job.SELECT){
            getCategory();
            System.out.println("Table selected");
        }       
        return null;
    }   
    @Override()
    public void done(){


    }

    public List<Category> getCategory() throws SQLException
    {
        cat = new ArrayList<Category>();
        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);
        }

        results.close();
        selectStatement.close();

        return cat;

    }
}

View只检索模型中的数据:

        people = model.getPeople();
        for (Category person : people) {
            tablemodel
                    .addRow(new Object[] { person.getId(), person.getName() });
        }

当你在Model.java类中调用SwingWorker时出现问题

public void load() throws Exception {
    people.clear();
    DAOFactory factory = DAOFactory.getFactory(DAOFactory.MYSQL);
    CategoryDAO personDAO = factory.getCategoryDAO();
    people.addAll(new MySQLCategoryDAO(Job.SELECT).execute());  - ERROR
}

错误: -

The method addAll(Collection<? extends Category>) in the type List<Category> is not applicable for the 
     arguments (void)

我知道SwingWorker什么都不返回,因为有错误。我应该在方法done()中编写代码,但我不知道如何解决它。

2 个答案:

答案 0 :(得分:2)

execute没有返回值,因此无法以您尝试使用它的方式使用它。 SwingWorker的想法是任务应该异步执行,所以你需要重新设计你的设计。

SwingWorker有结果(List<Category>),您需要:

  • 将结果放在SwingWorker内部(例如使用发布机制)
  • 或从外部致电get等待任务完成并返回。

以下是审核指南:http://docs.oracle.com/javase/tutorial/uiswing/concurrency/worker.html

快速举例:

class MySQLCategoryDAO extends SwingWorker<Void, Category> {

    // ...

    private List<Category> list; // do not modify inside doInBackground

    MySQLCategoryDAO(Job job, List<Category> list) {
        this.list = list;

        // ...
    }

    @Override
    protected Void doInBackground() {

        // ...

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

            publish(new Category(id, name)); // publish results to the EDT
        }

        // ...

        return null;
    }

    @Override
    protected void process(List<Category> chunks) {
        list.addAll(chunks); // add results to the list on the EDT

        // add to the JTable (?)
    }
}

public void load() throws Exception {
    people.clear();
    DAOFactory factory = DAOFactory.getFactory(DAOFactory.MYSQL);
    CategoryDAO personDAO = factory.getCategoryDAO();

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

如果要一次填充整个表,那么您也可以在循环后发布一个List,而不是一次发布一个Category。 process会收到一个带有单数元素的List<List<Category>>

答案 1 :(得分:0)

抱歉我的错误。 从视图获取到model.getPeople(),但没有返回任何内容。我做了一个测试:

但没有回复

 public class Model {

        private 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 {
    people.clear();
    DAOFactory factory = DAOFactory.getFactory(DAOFactory.MYSQL);

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