【发布时间】:2020-07-26 18:25:14
【问题描述】:
您好,我想在 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;
}
【问题讨论】: