在JAVA EE中无法正确读取ArrayList

时间:2019-12-15 14:10:05

标签: java object servlets arraylist

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

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

2 个答案:

答案 0 :(得分: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);

        }

答案 1 :(得分:0)

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

您要创建一个产品,对其进行更改,将其添加到列表中,然后然后更改该产品并再次添加。

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

Product liquide = new Liquid();

到循环内部。