在JAVA EE中无法正确读取ArrayList

问题描述 投票:0回答:2

我试图理解为什么将我通过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]

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

java object servlets arraylist
2个回答
0
投票

您在每次迭代中都覆盖相同的对象。您必须在迭代中创建一个新对象。因此,构造函数调用必须实例化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);

        }

0
投票

showEntireStock中,您只创建一个Product并将其添加到ArrayList

您正在创建产品,对其进行更改,将其添加到列表中,然后更改该产品并再次添加它

您需要在循环的每次迭代中创建一个新产品。移动

Product liquide = new Liquid();

到循环内部。

© www.soinside.com 2019 - 2024. All rights reserved.