Results 1 to 3 of 3
  1. #1
    yss1287b is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default List prints only the first row repeatedly in a loop

    Hi,

    I am trying to print some data from a database, passing the data through a List from a servlet to the jsp and then trying to print the data in the jsp.The data is properly available in the servlet , but only the first row of the data is printing repeatedly in the Jsp. Can any one help, Please?


    This is my serlvet


    package com.example.web;

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.List;

    import javax.servlet.RequestDispatcher;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    public class InfoServlet extends HttpServlet {

    public InfoServlet() {
    super();
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {


    String dbURL = "jdbc:derby://localhost:1527/myeclipse;create=true;username=classiccars;passwor d=admin";
    Connection conn = null;
    Statement stmt = null;
    ResultSet rs = null;
    PreparedStatement ps;

    String uname=null;
    String fname=null;
    String lname=null;
    String state=null;
    String address=null;
    String age=null;

    List<FormBean> alist=new ArrayList<FormBean>();

    FormBean fb=new FormBean();

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();


    String state1=request.getParameter("state");
    //out.println("State is "+state1);


    try {
    Class.forName("org.apache.derby.jdbc.ClientDriver" ).newInstance();
    conn = DriverManager.getConnection(dbURL);
    stmt = conn.createStatement();

    String sql="SELECT * FROM APP.USER_DATA WHERE STATE=?";
    //out.println("inside try");

    ps=conn.prepareStatement(sql);
    ps.setString(1,state1);
    rs=ps.executeQuery();

    while (rs.next()) {

    uname=rs.getString("UNAME");
    fname=rs.getString("FNAME");
    lname=rs.getString("LNAME");
    address=rs.getString("ADDRESS");
    state=rs.getString("STATE");
    age=rs.getString("AGE");

    fb.setUname(uname);
    fb.setFname(fname);
    fb.setLname(lname);
    fb.setAddress(address);
    fb.setState(state);
    fb.setAge(age);

    alist.add(fb);

    out.println(uname);

    }
    request.setAttribute("List",alist);

    RequestDispatcher dispatcher=getServletContext().getRequestDispatche r("/Output.jsp");
    dispatcher.forward(request,response);



    stmt.close();
    conn.close();

    } catch (InstantiationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }


    This is my output Jsp

    <body>

    <table>
    <%
    FormBean fb=new FormBean();
    List list=(List)request.getAttribute("List");
    int i;

    for (i=0;i< list.size() ;i++)
    {

    fb=(FormBean)list.get(i);

    %>

    <%= fb.getUname() %>
    <%= fb.getFname() %>


    <%

    } %>

    </table>

    <br>
    </body>
    </html>

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,120
    Rep Power
    20

    Default

    You are only creating one FormBean, and then changing the values in it each time round the loop, adding that same FormBean to the List each time. Meaning you have a List full of a single FormBean.

    This:
    FormBean fb=new FormBean();
    should be inside your while loop.

  3. #3
    yss1287b is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default

    Thank you so much. I am new to programming.This would definitely help.

Similar Threads

  1. Count each cell as it prints.
    By Shyamz1 in forum New To Java
    Replies: 9
    Last Post: 10-29-2010, 01:58 AM
  2. halp! reading repeatedly from a url...
    By tandren544 in forum Advanced Java
    Replies: 10
    Last Post: 07-16-2010, 10:20 AM
  3. Replies: 2
    Last Post: 11-22-2009, 06:24 PM
  4. Replies: 1
    Last Post: 10-20-2008, 08:35 AM
  5. Doclet that prints out all members of the class
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 12:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •