Java向类添加列表并进行修改
我的名单有问题。将列表添加到类中。在类列表中添加了更多项。当您想要显示类的列表时,不要有任何项 向MySQLCategoryDAO添加人员->在MySQLCategoryDAO中修改->在类模型中返回人员Java向类添加列表并进行修改,java,Java,我的名单有问题。将列表添加到类中。在类列表中添加了更多项。当您想要显示类的列表时,不要有任何项 向MySQLCategoryDAO添加人员->在MySQLCategoryDAO中修改->在类模型中返回人员 public class Model { public List<Category> people = new Vector<Category>(); public List<Category> getPeople() {
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();
}
}
您没有在任何地方调用process方法。。这是故意的吗?方法过程只是将数据添加到列表中。他想进入其他班级的名单。此问题的解决方案:在注释中。
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!
}
}
}
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();
}