填写一张桌子?

时间:2014-01-07 17:40:00

标签: java vaadin vaadin7

我有一个DAO,它返回我的数据库的一些信息。我想在表中显示该信息但不起作用。信息未显示。

我正在尝试这个。

//DAO
public class CustomerDAO {
     private Connection con = connection.getConnection();
     public List<Customer> getCustomer(){
          List<Customer> list = new ArrayList<Customer>();
          PreparedStatement stm = this.con.prepareStatement("SELECT * FROM customer");
          ResultSet rs = stm.executeQuery();
          while(rs.next()){
               Customer c = new Customer();
               c.setName(rs.getString("name"));
               list.add(c);
          }
          return list;
     }
}

// customer view
public class CustomerView extends CustomComponents{
     private Table table;
     private List<Customer> list;

     public CustomerView(){
          table = new Table();
          table.setSizeFull();
          tabela.addContainerProperty("Customer", String.class, null);
          getCustomer();
     }

     /** populate table */
     private void getCustomer(){
        list = new CustomerDAO().getCustomer();
        if(!list.isEmpty()){
            for(Customer c : list){
                 table.addItem(new Object[]{c.getName()}, 1);
            } 
        }
     }
}

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您的getCustomer()方法实施得不是很正确。

尝试:

private void getCustomer() {
    list = new CustomerDAO().getCustomer();
    if(!list.isEmpty()){
        for(Customer c : list){
             table.addItem(new Object[] { c.getName() }, null);
        } 
    }
 }

您可以找到方法说明here

另一种解决方案:

private void getCustomer() {
    list = new CustomerDAO().getCustomer();
    if(!list.isEmpty()){
        for(Customer c : list){
            Object addItem = table.addItem();
            table.getItem(addItem).getItemProperty("Customer").setValue(c.getName());
        } 
    }
 }