你好,我想要在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;
}
感謝alott。工作 – 2012-08-16 08:54:33