Ragataj
Ragataj

Reputation: 69

only last row of the database display repeatedly in jsp page

this is home servlet to display the list of data in jsp(java server page).I am not able to display my database table rows,instead of which the last row of the database was displayed repeatedly upto the total number of database rows.

 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String address = "balaju";
    request.setAttribute("address", address);//response can not use

    //ItemBean item = new ItemBean();

    List<ItemBean> itemList = new ArrayList<ItemBean>();
    try {
        System.out.println("at home servlet");
        ItemDao dao = ItemDaoFactory.getItemDao();
        itemList = dao.getItemFromdb();
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("@home servlet ");
    for(ItemBean item1:itemList){
        System.out.println(item1.getTransactionTime());
        System.out.println(item1.getItemPrice());
        System.out.println(item1.getItemName());
    }
    request.setAttribute("itemList", itemList);
    RequestDispatcher rd = request.getRequestDispatcher("home/home.jsp");
    rd.forward(request, response);}

//jdbc code intend to display list of data form database but only the last row of database is displaye in the jsp page using jstl foreach tag

 public List<ItemBean> getItemFromdb() throws SQLException {
    JdbcConnection connecton = new JdbcConnection();
    String query = "select * from itemsinfo";
    //Connection con = connecton.getConnection();
    List<ItemBean> itemList = new ArrayList<ItemBean>();
    ItemBean item = new ItemBean();
    Statement statement = connecton.getSqlStatement();
    ResultSet rs;
    try {
       rs = statement.executeQuery(query);
         while (rs.next()) {


            item.setItemName(rs.getString("itemname"));
            item.setItemPrice(rs.getDouble("itemprice"));
            item.setTransactionTime(rs.getString("transactiontime"));
           // itemList.add(item);

           /* System.out.println("itemname :"+item.getItemName());
            System.out.println("price:"+item.getItemPrice());
            System.out.println("time:"+item.getTransactionTime());
            System.out.println("/n"); */

        }

        System.out.println("at jdbc code");
        for(ItemBean item1:itemList){

            System.out.println(item1.getItemName());
            System.out.println(item1.getItemPrice());
            System.out.println(item1.getTransactionTime());
        }

    } catch (SQLException e) {
        e.printStackTrace();
    }
    return itemList;

}

Upvotes: 0

Views: 1239

Answers (1)

Elliott Frisch
Elliott Frisch

Reputation: 201467

You only have one Item in your List.

 // ItemBean item = new ItemBean(); // You don't need this one.
 while (rs.next()) {
   ItemBean item = new ItemBean(); // Instantiate a new ItemBean, don't update ONE.
   item.setItemName(rs.getString("itemname"));
   item.setItemPrice(rs.getDouble("itemprice"));
   item.setTransactionTime(rs.getString("transactiontime"));
   itemList.add(item); // Add the item to the List.
 }

Upvotes: 2

Related Questions