gpt4 book ai didi

java - 无法在 JAVA EE 中正确读取 ArrayList

转载 作者:行者123 更新时间:2023-12-01 19:22:29 25 4
gpt4 key购买 nike

我试图理解为什么我从 SQL 查询构建的 ArrayList 在传递给 Servlet 时无法正确读取。

public ArrayList<Product> showEntireStock() throws BusinessException{

ArrayList<Product> listeProduct = new ArrayList<Product>();


try(Connection cnx =ConnectionProvider.getConnection(); PreparedStatement st = cnx.prepareStatement(SHOW_ALL);) {
ResultSet rs= null;
Product liquide = new Liquid();


rs= st.executeQuery();

while(rs.next()) {

liquide.setNom(rs.getString("nameProduct"));
liquide.setDesignation(rs.getString("designationProduct"));
liquide.setCategory(rs.getInt("categorieProduct"));
liquide.setId(rs.getInt("idProduct"));
**System.out.println(liquide.toString());**
listeProduct.add(liquide);

}

}catch(Exception e) {


BusinessException busy = new BusinessException();
busy.ajouterErreur(ListeCodeErreur.STOCK_VIDE);
busy.printStackTrace();
}

return listeProduct;

}

此方法中的 System.out.println() 确实显示属性已填充正确的信息(此处只有 id 可以更改,其他属性已硬编码用于测试)

Product [id=2, nom=Champagne, designation=nouveau liquide, imagePath=null, price=0.0, category=1]
Product [id=3, nom=Champagne, designation=nouveau liquide, imagePath=null, price=0.0, category=1]
Product [id=4, nom=Champagne, designation=nouveau liquide, imagePath=null, price=0.0, category=1]
Product [id=5, nom=Champagne, designation=nouveau liquide, imagePath=null, price=0.0, category=1]
Product [id=6, nom=Champagne, designation=nouveau liquide, imagePath=null, price=0.0, category=1]
Product [id=7, nom=Champagne, designation=nouveau liquide, imagePath=null, price=0.0, category=1]
Product [id=8, nom=Champagne, designation=nouveau liquide, imagePath=null, price=0.0, category=1]
Product [id=9, nom=Champagne, designation=nouveau liquide, imagePath=null, price=0.0, category=1]
Product [id=10, nom=Champagne, designation=nouveau liquide, imagePath=null, price=0.0, category=1]

但是当我尝试在 Servelt 中读取它时,ArrayList 中包含的 Product 对象的 toString() 方法仅显示最后一个元素,并根据 ArrayList 中的对象数量打印它。

@WebServlet("/Stock")
public class Stock extends HttpServlet {
private static final long serialVersionUID = 1L;



protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

StockDAOImpl stock = new StockDAOImpl();
ArrayList<Product> listeProduct = new ArrayList<>();


try {
listeProduct = stock.showEntireStock();
request.setAttribute("stock", listeProduct);

for (Product p : listeProduct) {

System.out.println(p.toString());
}
} catch (BusinessException e1) {

e1.printStackTrace();
}


this.getServletContext().getRequestDispatcher("/WEB-INF/Stock.jsp").forward(request, response);


}
   Product [id=10, nom=Champagne, designation=nouveau liquide, imagePath=null, price=0.0, category=1]
Product [id=10, nom=Champagne, designation=nouveau liquide, imagePath=null, price=0.0, category=1]
Product [id=10, nom=Champagne, designation=nouveau liquide, imagePath=null, price=0.0, category=1]
Product [id=10, nom=Champagne, designation=nouveau liquide, imagePath=null, price=0.0, category=1]
Product [id=10, nom=Champagne, designation=nouveau liquide, imagePath=null, price=0.0, category=1]
Product [id=10, nom=Champagne, designation=nouveau liquide, imagePath=null, price=0.0, category=1]
Product [id=10, nom=Champagne, designation=nouveau liquide, imagePath=null, price=0.0, category=1]
Product [id=10, nom=Champagne, designation=nouveau liquide, imagePath=null, price=0.0, category=1]

我不知道为什么要这样做,也找不到可以给出提示的文章。也没有错误消息。感谢您的帮助。

最佳答案

您在每次迭代中都覆盖相同的对象。您必须在迭代中创建一个新对象。因此构造函数调用必须插入 while 循环

        rs= st.executeQuery();

while(rs.next()) {
Product liquide = new Liquid();

liquide.setNom(rs.getString("nameProduct"));
liquide.setDesignation(rs.getString("designationProduct"));
liquide.setCategory(rs.getInt("categorieProduct"));
liquide.setId(rs.getInt("idProduct"));
**System.out.println(liquide.toString());**
listeProduct.add(liquide);

}

关于java - 无法在 JAVA EE 中正确读取 ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59344710/

25 4 0