简体   繁体   中英

can't read ArrayList properly in JAVA EE

I try to understand why an ArrayList that I've built from an SQL query won't be read properly when passed to a 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;

}

The System.out.println() in this method does show that attributes are getting filled with the right information (only the id can change here, the other attributes are hard coded for test)

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]

BUT when I try to read it in a Servelt, the toString()method of the Product object contained in the ArrayList only shows the last element and print it as many times as there are objects in the 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]

I have no idea why it's doing this and can't find an article that would give a hint.No error message either. Thank you for your help.

You override the same object in every iteration. You have to create a new object in the iteration. So the constructor call must instide the while loop

        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);

        }

In showEntireStock , you only create and add a single Product to ArrayList .

You're creating a product, changing it, adding it to the list, then changing that same product and adding it again .

You need to create a new product on each iteration of the loop. Move

Product liquide = new Liquid();

to the inside of the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM