Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

while loop in a while loop

 
Sujay Nadkarni
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two resultsets. This is the code
while(rs.next()) { //loop1

while(rs2.next()) {
//loop 2
}

}
Is this possible. Below is my code

This is the error that i get..
An exception occurred processing JSP page /computation.jsp at line 42. Line 42 = while(rsi.next()){

Help me out..thank you!!
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First time thru the outer loop you get the first result from that query.
The second loop then reads ALL the results from the second query.
You then get the second result from the first query and then try to read the next result from the second query but there are none left because you read them all the first time thru the outer loop.
What i think you probably want is something like

Of course this assumes both queries return the same number of results. If they don't then you will have to add some extra code to handle that situation.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot have more than one opened resultset per statement. If you open another resultset on the same statement, the previously opened one will be closed.

To fix this, either merge two queries to one query (the SQL JOIN clause may be of use), or process the first resultset before opening the second, or create two independent statements. Those suggestions are in the order of recommendation.

This question in fact belongs in the JDBC forum by the way.

Oh, in the future please paste the complete exception message and trace. Those are very, very important to know, because they tell exactly what the cause of the problem is and how it is happened and so this makes clear how to solve it. Otherwise others will make assumptions about the cause of the problem which may not be true at all.
 
Sujay Nadkarni
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no both the queries dont return the same number of results!!
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not the actual cause of the problem.

Please read the exception message and trace.
 
Sujay Nadkarni
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i did this


This gives me the exact result as Select SUM(mcap) from table_name

now I add this


I get this error then
An error occurred at line: 48 in the jsp file: /computation.jsp
Unreachable code
45: float x=p+p;
46: break;
47:
48: float y=p/x;
49: out.println(y);
50:
51: }


Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:423)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:316)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:294)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:281)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:337)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sujay Nadkarni wrote:no both the queries dont return the same number of results!!


Well that's irrelevant now if you read Bauke's post. I've never used ResultSets so I was making an assumption about how they behave.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sujay Nadkarni wrote:
I get this error then
An error occurred at line: 48 in the jsp file: /computation.jsp
Unreachable code
45: float x=p+p;
46: break;
47:
48: float y=p/x;
49: out.println(y);
50:
51: }


What, exactly, do you think the break statement does?
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I should add although this is told you more than often before, you should NOT (I repeat: NOT) write raw Java code in JSP files. It costs too MUCH time to develop, test, maintain and debug. It is simply asking for trouble for starters. Write raw Java code in a simple Java class and test/run it as a Java application using main(). This is soo much faster and easier.

Get it all to work in Java classes first. Once done that, just let a Servlet invoke those classes, store the results in the desired scope and use a JSP to display them. That's all what they ought to do.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic