• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

while loop in a while loop

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!!
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic