从数据库读取数据并存储在数组列表对象中
大家好,我想在html页面上显示我的数据库表的全部内容。我试图首先从数据库获取记录并存储,但是当我在html页面上返回数组列表时,它只显示最后一条记录作为数据库表的计数。以下是代码:ArrayList
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;
}