2012-08-16 54 views
8

你好,我想要在html页面上显示我的数据库表的全部内容。我试图从数据库中首先获取记录,并存储在ArrayList但当我在html上返回数组列表页面只显示最后一条记录作为我的数据库表的数量。 这里是下面的代码:从数据库读取数据和存储在数组列表对象

public ArrayList<CustomerDTO> getAllCustomers() 
{ 
    ArrayList<CustomerDTO> customers = new ArrayList<CustomerDTO>(); 
    CustomerDTO customer = null; 
    Connection c; 
    try { 
     c = openConnection(); 
     Statement statement = c.createStatement(); 
     String s = "SELECT * FROM customer"; 

     ResultSet rs = statement.executeQuery(s); 
     int g =0; 

     while (rs.next()) { 

      customer.setId(rs.getInt("id")); 
      customer.setName(rs.getString("name")); 

      customer.setAddress(rs.getString("address")); 
      customer.setPhone(rs.getString("phone")); 
      customer.setEmail(rs.getString("email")); 
      customer.setBountPoints(rs.getInt("bonuspoint")); 
      customer.setTotalsale(rs.getInt("totalsale")); 

      customers.add(customer); 
     } 

     rs.close(); 
    } catch (Exception e) { 
     System.out.println(e); 
    } 

    return customers; 
} 

回答

0

正在重用的customer参考。 Java通过Obejcts的参考工作。不适用于基元。

你正在做什么是添加到列表中相同的customer,然后修改它。因此,为所有对象设置相同的值。这就是为什么你看到最后一个。因为都是一样的。

while (rs.next()) { 
     Customer customer = new Customer(); 
     customer.setId(rs.getInt("id")); 

     ... 
4

您必须在每次迭代中创建一个新的客户对象,然后在迭代过程中将该新创建的对象添加到ArrayList中。

+0

感谢alott。工作 – 2012-08-16 08:54:33

2

尝试每次创建客户的新实例,例如

  while (rs.next()) { 

     Customer customer = new Customer(); 
     customer.setId(rs.getInt("id")); 
     customer.setName(rs.getString("name")); 

     customer.setAddress(rs.getString("address")); 
     customer.setPhone(rs.getString("phone")); 
     customer.setEmail(rs.getString("email")); 
     customer.setBountPoints(rs.getInt("bonuspoint")); 
     customer.setTotalsale(rs.getInt("totalsale")); 

     customers.add(customer); 


    } 
0

我试图从数据库中第一和商店的ArrayList 取记录,但是当我的HTML页面返回数组列表,它只显示最后一个记录 多次为我的数据库表的计数

这部分大部分已被所有以前的答案覆盖。因此,您需要在while循环内创建CustomerDTO的新实例,并将其添加到您的ArrayList

有,我想点评一下一两件事:

  • 确保你释放所有的资源,你使用它们完成后。从您发布的代码中,您尚未关闭您的Statement或您的Connection对象(不太确定您是否在池中连接,在这种情况下,您需要将此连接释放到池

所以,当你考虑这些问题,你的代码的结构可能是这个样子:

public ArrayList<CustomerDTO> getAllCustomers() 
{ 
    ArrayList<CustomerDTO> customers = new ArrayList<CustomerDTO>(); 
    Connection c = null; 
    Statement statement = null; 
    ResultSet rs  = null; 

    try { 
     c   = openConnection(); 
     statement = c.createStatement(); 
     String s = "SELECT * FROM customer"; 

     rs   = statement.executeQuery(s); 
     int g =0; 

     while (rs.next()) { 
      CustomerDTO customer = new CustomerDTO(); 
      //Code to fill up your DTO 
      customers.add(customer); 
     } 
    } catch (Exception e) { 
     System.out.println(e); 
    }finally{ 
     //Code to release your resources 
    } 

    return customers; 
} 
0

如果您的客户类有静态变量删除它们,以便类应该是这个样子。

public class customer { 

    private int id; 
    private String name; 
    private String DOB; 

    public int getId() { 
     return id; 
    } 
    public String getName() { 
     return name; 
    } 
    public String getDOB() { 
     return DOB; 
    } 
    public void setId(int id) { 
     this.id = id; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public void setDOB(String dOB) { 
     this.DOB = dOB; 
    } 

,而不是像

public class customer { 

    private static int id; 
    private static String name; 
    private static String DOB; 

    public static int getId() { 
     return id; 
    } 
    public static String getName() { 
     return name; 
    } 
    public static String getDOB() { 
     return DOB; 
    } 
    public static void setId(int id) { 
     custumer.id = id; 
    } 
    public static void setName(String name) { 
     customer.name = name; 
    } 
    public static void setDOB(String dOB) { 
     customer.DOB = dOB; 
    } 
8

用下面的代码

public static ArrayList<Customer> getAllCustomer() throws ClassNotFoundException, SQLException { 
    Connection conn=DBConnection.getDBConnection().getConnection(); 
    Statement stm; 
    stm = conn.createStatement(); 
    String sql = "Select * From Customer"; 
    ResultSet rst; 
    rst = stm.executeQuery(sql); 
    ArrayList<Customer> customerList = new ArrayList<>(); 
    while (rst.next()) { 
     Customer customer = new Customer(rst.getString("id"), rst.getString("name"), rst.getString("address"), rst.getDouble("salary")); 
     customerList.add(customer); 
    } 
    return customerList; 
} 

尝试,这是我的模型类

public class Customer { 
private String id; 
private String name; 
private String salary; 
private String address; 
public String getId() { 
    return id; 
} 
public void setId(String id) { 
    this.id = id; 
} 
public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
public String getSalary() { 
    return salary; 
} 
public void setSalary(String salary) { 
    this.salary = salary; 
} 
public String getAddress() { 
    return address; 
} 
public void setAddress(String address) { 
    this.address = address; 
} 
} 

这是我的观点方法

private void reloadButtonActionPerformed(java.awt.event.ActionEvent evt) {            
    try { 
     ArrayList<Customer> customerList = null; 
     try { 
      try { 
       customerList = CustomerController.getAllCustomer(); 
      } catch (SQLException ex) { 
       Logger.getLogger(veiwCustomerFrame.class.getName()).log(Level.SEVERE, null, ex); 
      } 
     } catch (Exception ex) { 
      Logger.getLogger(ViewCustomerForm.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     DefaultTableModel tableModel = (DefaultTableModel) customerTable.getModel(); 
     tableModel.setRowCount(0); 
     for (Customer customer : customerList) { 
      Object rowData[] = {customer.getId(), customer.getName(), customer.getAddress(), customer.getSalary()}; 
      tableModel.addRow(rowData); 
     } 


    } catch (Exception ex) { 
     Logger.getLogger(ViewCustomerForm.class.getName()).log(Level.SEVERE, null, ex); 
    } 

} 
0

Instead of null , use CustomerDTO customers = new CustomerDTO()`;

CustomerDTO customer = null; 


    private static List<Author> getAllAuthors() { 
    initConnection(); 
    List<Author> authors = new ArrayList<Author>(); 
    Author author = new Author(); 
    try { 
     stmt = (Statement) conn.createStatement(); 
     String str = "SELECT * FROM author"; 
     rs = (ResultSet) stmt.executeQuery(str); 

     while (rs.next()) { 
      int id = rs.getInt("nAuthorId"); 
      String name = rs.getString("cAuthorName"); 
      author.setnAuthorId(id); 
      author.setcAuthorName(name); 
      authors.add(author); 
      System.out.println(author.getnAuthorId() + " - " + author.getcAuthorName()); 
     } 
     rs.close(); 
     closeConnection(); 
    } catch (Exception e) { 
     System.out.println(e); 
    } 
    return authors; 
} 
0
while (rs.next()) { 

      customer.setId(rs.getInt("id")); 
      customer.setName(rs.getString("name")); 

      customer.setAddress(rs.getString("address")); 
      customer.setPhone(rs.getString("phone")); 
      customer.setEmail(rs.getString("email")); 
      customer.setBountPoints(rs.getInt("bonuspoint")); 
      customer.setTotalsale(rs.getInt("totalsale")); 

      customers.add(customer); 
      customer = null; 
     } 

尝试用上面提到的代码替换while循环代码。这里我们所做的是在做customers.add(customer)我们正在做的客户= NULL之后;`

0

创建CustomerDTO对象中每次while循环

检查下面的代码

while (rs.next()) { 

    Customer customer = new Customer(); 

    customer.setId(rs.getInt("id")); 
    customer.setName(rs.getString("name")); 
    customer.setAddress(rs.getString("address")); 
    customer.setPhone(rs.getString("phone")); 
    customer.setEmail(rs.getString("email")); 
    customer.setBountPoints(rs.getInt("bonuspoint")); 
    customer.setTotalsale(rs.getInt("totalsale")); 

    customers.add(customer); 
} 
相关问题