aspose file tools*
The moose likes Java in General and the fly likes while loop in a while loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "while loop in a while loop" Watch "while loop in a while loop" New topic
Author

while loop in a while loop

Sujay Nadkarni
Ranch Hand

Joined: Jul 03, 2008
Posts: 34
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

Joined: Aug 05, 2005
Posts: 3681
    
  16
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.


Joanne
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Jul 03, 2008
Posts: 34
no both the queries dont return the same number of results!!
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
That's not the actual cause of the problem.

Please read the exception message and trace.
Sujay Nadkarni
Ranch Hand

Joined: Jul 03, 2008
Posts: 34
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

Joined: Aug 05, 2005
Posts: 3681
    
  16
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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?


Steve
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: while loop in a while loop