Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > JSP Basics
|
JSP Basics Beginning-level questions on JSP. More advanced coders should post to Pro JSP.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the JSP Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 17th, 2005, 09:50 AM
Authorized User
 
Join Date: Oct 2004
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Default print multiple times..multiple rows...

this below code is used for the retrieving values from the table.

The tables have several rows...


my prob.. is that when i retrieve more than one rows values..it print two times the same rows..

if i retrieve 3 rows values..it prints three times the same rows..

plz help me in my code....where is the prob...plz...

code:-

while(results.next()){



for (int j = 1; request.getParameter((j==1)?"Ac.code":("Ac.code_" +j)) != null; j++) {


out.println("<tr><td> <INPUT TYPE='TEXT' NAME='ac_code' size='12' value='" +request.getParameter((j==1)?"Ac.code":("Ac.code_" +j)) + "' readonly </td>");

out.println("<td><INPUT TYPE='TEXT' NAME='description' size='12' value='" + request.getParameter((j==1)?"description":("descri ption_" +j))+ "' readonly </td>");

out.println("<td> <INPUT TYPE='TEXT' NAME='description' size='12' value='" + request.getParameter((j==1)?"description1":("descr iption1_" +j)) + "' readonly </td>");
out.println("</tr> ");
}

 
Old January 18th, 2005, 11:08 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am not sure without, but I would say the error is with the following line:

for (int j = 1; request.getParameter((j==1)?"Ac.code":("Ac.code_" +j)) != null; j++) {


What is this line for?
Is one of the parameters the number of records to be added, if so when you pass 2 you will loop twice, if you pass 3 you will loop 3 times.

Perhaps your while loop should be inside this one?

Like I say am not sure just by looking at this code, but am pretty sure the problem is with one of your loops.

 
Old January 18th, 2005, 07:08 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello again,

Had another look at this and....

What is the relationship between

while(results.next()){

and

request.getParameter

Your while loop is obviously looping database records, so for each record in a database you are running the for loop.

Your for loop is extracting ALL of the parameters for each pass of the loop, so if you pass 3 parameters you are looping like this:

While (record 1)
    getParameter 1
    getParameter 2
    getParameter 3
While (record 2)
    getParameter 1
    getParameter 2
    getParameter 3
While (record 3)
    getParameter 1
    getParameter 2
    getParameter 3
While (record 4)
    no more getParameter
While (record 5)
    no more getParameter

Until the while loop has reached the end of the record set.



I do not think you need the for loop:

for (int j = 1; request.getParameter((j==1)?"Ac.code":("Ac.code_" +j)) != null; j++)


I would suggest you try using an IF statement to test get.parameter

Then at the END of the WHILE loop add j++

while(results.next()){

if (getParameter("Ac.code_" +j)) != null))
{

out.println("<tr><td> <INPUT TYPE='TEXT' NAME='ac_code'size='12' value='" +request.getParameter((j==1)?"Ac.code":("Ac.code_" +j)) + "' readonly </td>");

out.println("<td><INPUT TYPE='TEXT' NAME='description' size='12' value='" + request.getParameter((j==1)?"description":("descri ption_" +j))+ "' readonly </td>");

out.println("<td> <INPUT TYPE='TEXT' NAME='description' size='12' value='" + request.getParameter((j==1)?"description1":("descr iption1_" +j)) + "' readonly </td>");
out.println("</tr> ");

} // end if

j++;

} // end while

 
Old January 18th, 2005, 07:11 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 204
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry,

I should have added that this was caused in part by:

int j = 1

This part of your for loop reset the loop each time it was run, thus all parameters are returned on each pass of the while loop









Similar Threads
Thread Thread Starter Forum Replies Last Post
Incl certain fields if they appear multiple times SpyderSL Access VBA 1 November 4th, 2008 12:52 PM
same records shown multiple times alie VB Databases Basics 1 March 8th, 2006 10:13 AM
Problem with loading user control multiple times neha mukerjee ASP.NET 2.0 Basics 0 February 24th, 2006 06:11 AM
Button Handler Being called multiple times ExDb VB Databases Basics 1 January 22nd, 2006 07:32 PM
Updating multiple Rows from multiple fields in ASP vdm_nana SQL Server ASP 0 April 1st, 2004 04:26 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.