2012-08-16 97 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); 
} 
相關問題