1

I want to retrieve data from database using servlet and show it into a jsp file ,but what is retrieved is only the first row of the table.I want to be retrieved all rows.Could anyone help me? Thanks,and here is my code:

Servlet:

    try{

        DBConnection connection = new DBConnection();
        Connection con = connection.Connect();
        ResultSet rs;
        Statement st = con.createStatement();
        java.sql.PreparedStatement ps=con.prepareStatement("SELECT Nume,Descriere,Ingrediente,Mod_preparare,Imagini,Total_grasimi,Total_carbohidrati,Total_proteine,Total_calorii FROM reteta");
        rs=ps.executeQuery();

        while(rs.next()){
            //name
            String Nume = rs.getString("Nume");
            request.setAttribute("Nume",Nume);

            //descripton
            String Descriere = rs.getString("Descriere");
            request.setAttribute("Descriere",Descriere);

            //images
            String Imagini = rs.getString("Imagini");
            request.setAttribute("Imagini",Imagini);


       request.getRequestDispatcher("/retete.jsp").forward(request, response);


        }

    rs.close();
    st.close();
    con.close();
    }
    catch (Exception e2)
    {
      e2.printStackTrace();
    }

    finally
    {
      out.close();
    }
}

Jsp:

        <a href="images/{Imagini} " class="gall_item"><img src="images/${Imagini}" alt=""></a>
        <div class="gall_bot">
        <div class="text1"><a href="#">${Nume} </a></div>
        ${Descriere} 
        <br>
        <button type = "submit" class = "btn" >more</button>
        </div>
      </div>
    </div>
  </div>
Bogdan
  • 155
  • 2
  • 3
  • 10

1 Answers1

2

You are setting attributes and forwarding the request within the while loop: that's probably why you are not getting the result you are expecting

You should use each field to create an object and each time around the loop store the newly created object in a collection (array, arraylist, linkedlist map...the choice is yours), then outside the loop create an attribute whose value is the collection populated with the object created in the while loop and finally forward the request to the Jsp

// create a new collection be it an array, arrayList, linkedList....
YourCollection yc = new YourCollection();

while(rs.next()){
        //name
        String Nume = rs.getString("Nume");
        //descripton
        String Descriere = rs.getString("Descriere");
        //images
        String Imagini = rs.getString("Imagini");

        // create object with db data;
        YourClass yourObject = new YourClass(Nume, Descriere, Imagini);
        // add object to collection
        YourCollection.add(yourObject);
}
// create attribute from collection
request.setAttribute("YourCollection", yc);

request.getRequestDispatcher("/retete.jsp").forward(request,response);

Within the Jsp you will be able to easily access the attribute and loop through the collection with a foreach JSTL tag (for instance) and the instance variables of each object via expression language (I used the dot notation in the example)

<c:foreach var="yourVarName" items="${YourCollection}">

            <a href="images/{yourVarName.imagini} " class="gall_item"><img src="images/${yourVarName.imagini}" alt=""></a>
            <div class="gall_bot">
            <div class="text1"><a href="#">${yourVarName.nume} </a></div>
    ${yourVarName.descriere} 
            <br>
            <button type = "submit" class = "btn" >more</button>
             <!-- check closing divs.... -->
            </div>
          </div>
        </div>
      </div>

</c:foreach>

Note: the properties accessed via dot notation correspond to the instance variables of each object and need not to be capitalized in compliance with the javabean specification.

Angelo Oparah
  • 673
  • 1
  • 7
  • 18
  • Modified as you said: Servlet --> ArrayList datalist = new ArrayList(); while(rs.next()){ datalist.add(rs.getString("Nume")); datalist.add(rs.getString("Descriere")); datalist.add(rs.getString("Imagini")); } request.setAttribute("Ehealth", datalist); request.getRequestDispatcher("/retete.jsp").forward(request, response); – Bogdan Apr 12 '16 at 20:00
  • JSP->
    ${var.Descriere} But I have this error:javax.el.PropertyNotFoundException: Property 'Nume' not found on type java.lang.String
    – Bogdan Apr 12 '16 at 20:04
  • Did u get any error? – Angelo Oparah Apr 12 '16 at 20:08
  • SEVERE: Servlet.service() for servlet jsp threw exception javax.el.PropertyNotFoundException: Property 'Nume' not found on type java.lang.String – Bogdan Apr 12 '16 at 20:12
  • You have not created an object. Properties belong to objects – Angelo Oparah Apr 12 '16 at 20:15
  • Define a Java class with instance variables Nume, Descriere and Imagini and a constructor that takes those 3 fields as parameters, then use each String retrieved by the database to create an object of the same class you have defined. Then populate the arraylist with those objects... – Angelo Oparah Apr 12 '16 at 20:25
  • Created a class where are get and set methods implemented for my db data.( Ehealth health = new Ehealth();)Added to my list (datalist.add(health);) and now I have this error : javax.el.PropertyNotFoundException: Property 'Imagini' not found on type beans.Ehealth – Bogdan Apr 12 '16 at 20:28
  • It has to be something like Ehealth health = new Ehealth(nume,descriere,imagini); – Angelo Oparah Apr 12 '16 at 20:32
  • You need a constructor that takes those 3 parameters and don't capital letters for the instance variables – Angelo Oparah Apr 12 '16 at 20:32
  • Can I send you an archive with my code and take a look?I'm beginner in programming and is hard to me to understand. – Bogdan Apr 12 '16 at 20:40
  • I have some network issues, can't connect via PC at the moment I'm texting through my phone...you might want to send me a txt file with your code in it – Angelo Oparah Apr 12 '16 at 20:44
  • Sent,Thank you very much! – Bogdan Apr 12 '16 at 20:56
  • I got it. Give me some time to go through it and I'll email you back – Angelo Oparah Apr 12 '16 at 20:58
  • Don't worry about that,take your time,Thank you! – Bogdan Apr 12 '16 at 20:59