0

I am trying access database table data using java bean propety and array list. I to convert array list to html table. I am unable to find any proper example. I don't want to use Servlet.

public class ItemBean {
    int item_id;
    String item_name;
    String item_description;
    double item_costpice;
    double item_unitpice;
    public void setItemId(int id){this.item_id=id;}  
    public int getItemId(){return item_id;}  

    public void setItemName(String name){this.item_name=name;}  
    public String getItemName(){return item_name;} 

    public void setItemDesc(String name){this.item_name=name;}  
    public String getItemDesc(){return item_name;} 

    public void setItemCostPrice(double price){this.item_costpice=price;}  
    public double getItemCostPrice(){return item_costpice;} 

    public void setItemUnitPrice(double price){this.item_unitpice=price;}  
    public double getItemUnitPrice(){return item_unitpice;} 

}

package pam.setup;
import java.sql.*;
import java.util.ArrayList;

public class Item {

    public static ArrayList<ItemBean> ItemList()
    {
        ResultSet rs = null;
        ArrayList<ItemBean> list=new ArrayList<ItemBean>();
        ItemBean dbitem = new ItemBean();
        try{


            Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection(  
                    "jdbc:mysql://localhost:3306/sonoo","root","");  

                Statement stmt=con.createStatement();  
                rs=stmt.executeQuery("select * from items");  
                con.close(); 
                while (rs.next()) {   
                    dbitem.setItemId(rs.getInt("item_id"));
                    dbitem.setItemName(rs.getString("name"));
                    dbitem.setItemDesc(rs.getString("description"));
                    dbitem.setItemCostPrice(rs.getDouble("cost_price"));
                    dbitem.setItemUnitPrice(rs.getDouble("unit_price"));
                    list.add(dbitem);
                }           
        }catch(Exception e){ System.out.println(e);}

        return list;

    }
}

<jsp:useBean id="items" scope="page" class="pam.setup.Item" type="pam.setup.Item">
    <table>
        <tbody>
        <c:forEach items="${item.itemList}" var="item">
            <tr><td>item.itemId</td><td>item.itemName</td></tr>
        </c:forEach>
        </tbody>
    </table>
</jsp:useBean>

I have written above code, I want to access database table data in jsp using java bean property and iterate data and generate HTML table. But I am getting mentioned below result instead of actual item id and name.

item.itemId item.itemName

  • 3
    Nobody will write anything for you unless you tried something yourself first. You will want to write the JSP and then come back if you bump into a problem. I think you don't understand what Stackoverflow was built for. – Andrei Sfat Dec 30 '17 at 09:20
  • This link must help you ..https://stackoverflow.com/questions/6395621/how-to-call-a-static-method-in-jsp-el – Mahesh_Loya Dec 30 '17 at 09:50

1 Answers1

0

New ItemBean must be created for each iteration of the loop:

while (rs.next()) { 
    ItemBean dbitem = new ItemBean();
    dbitem.setItemId(rs.getInt("item_id"));
    ...

Close the connection after the loop.

Use ${} to evaluate bean expression:

 <tr><td>item.itemId</td><td>${item.itemName}</td></tr>
Jeff Miller
  • 1,424
  • 1
  • 10
  • 19
  • I have done this, no change. In jsp file there is exclamation sign with fo each loop line of code and mention in tip that unknown tag c. I thin this may ma be the reason – murtaza Mughal Jan 02 '18 at 03:49
  • for unknown c tag I have added below lines and jstl.jar file in lib. %@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> < %@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x" %> – murtaza Mughal Jan 02 '18 at 19:52