tags:

views:

49

answers:

3

I have a jsp file which has the following code:

<% 
while(rs.next()){     //rs is the resultser

%>      
<tr bgcolor="#FFFFFF" onMouseOver="this.bgColor='gold';" onMouseOut="this.bgColor='#FFFFFF';">
<td width="3%"><span style="font-size: 8pt"><%=i=i+1%></span></td>
<td width="10%"><span style="font-size: 8pt">
<a href="cir_view.jsp?cir_id=<%=rs.getString("cir_id")%>

and I want to change the while loop to a for loop. I tried this but, I was stuck trying to retrieve the values for the cir_id. Here is my code:

<% 
for(int j=1; j<=totalCols; j++) {
%>      
<tr bgcolor="#FFFFFF" onMouseOver="this.bgColor='gold';" onMouseOut="this.bgColor='#FFFFFF';">
<td width="3%"><span style="font-size: 8pt"><%=j=j+1%></span></td>
<td width="10%"><span style="font-size: 8pt">
<a href="cir_view.jsp?cir_id=<%=rs.getString("cir_id")%>" >>> Here is the change??

Can you please help to modify it?

A: 

rs.next() is missing

Change your for loop to:

for(int j=1; j<=totalCols; j++,rs.next()) {

it could also be declared as

for(int j=1;rs.next(); j++) {
YoK
where i should keep it?and is there any modification must be done to rs.getString("cir_id")% at the end?
maas
totalcols is variable which is declared to limit the displaying of the records, suppose 5Should I remoce it?
maas
I used thisfor(int j=1; j<=totalCols j++) { and only the first record is displayed
maas
@maas did you try my solutions ?
YoK
A: 

ResultSet is meant to be iterated through in similar vein as Iterator which means that you need to call next() to move the "cursor" to next record in ResultSet.

Esko
Can you please clarify more and how to edit
maas
Thank you my friend.It worked
maas
@maas: Upvotes and accepted answer, then?
Esko
+1  A: 

Here's the code:

<% 
for(int j=1; j<=totalCols && rs.next(); j++) {
%>      
.....
<a href="cir_view.jsp?cir_id=<%=rs.getString("cir_id")%>"

You can keep rs.next() in the for loop itself

naikus
+1, but note the 'j=j+1' in loop body needs to be removed.
sje397
I tried it , it displayed only the first record
maas
@maas Change to j's code to "<%=(j+1)%>" in the body of your loop, that could be causing the problem
naikus
@sje397 thanks, I thought that totalCols was also important
naikus
Still it did not work
maas