www.webdeveloper.com
Results 1 to 2 of 2

Thread: JSP Resultset not iterating multiple times

Hybrid View

  1. #1
    Join Date
    Sep 2010
    Posts
    15

    JSP Resultset not iterating multiple times

    Hi,

    I'm trying to use JSP resultset.next() multiple times to print HTML tables. However, it doesn't seem to want to work. I can print one or the other fine, but not both. I'm trying something below:

    Resultset rs = null;
    String SQL = "select column1, column2 from table";
    rs = stmtPROD.executeQuery(SQL);

    out.println("<table>")
    while(rs.next()) {
    out.println("<tr><td> " + rs.getString("column1") + "</td></tr>");
    out.println("</table><table>");

    while(rs.next()) {
    out.println("<tr><td> " + rs.getString("column2") + "</td></tr>");
    out.println("</table>");

    Any tips would be helpful. Thanks!
    Last edited by RunningDude87; 02-10-2014 at 09:36 AM.

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,122
    what are you trying to do?
    you should create both tables in one while loop. The way I see it your code should be like this
    [code]
    out.println("<table>")
    while(rs.next()) {
    out.println("<tr><td> " + rs.getString("column1") + "</td></tr>");
    out.println("<tr><td> " + rs.getString("column2") + "</td></tr>");
    }
    out.println("</table>
    [/code
    If for some reasons you want each row in its own table element then it should be like this

    [code]

    while(rs.next()) {
    out.println("<table>")
    out.println("<tr><td> " + rs.getString("column1") + "</td></tr>");
    out.println("</table>");
    out.println("<table>")
    out.println("<tr><td> " + rs.getString("column2") + "</td></tr>");
    out.println("</table>");
    }
    [/code

    But using result set in that manner is not correct. Read up on what happens when you do rs.next()
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles