• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

execute select stament inside while(rs.next)

 
manohar gunturu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to execute two sql select statements it's like...

String Query1="SELECT * from sony.fil where sony.fil.private is null or sony.fil.private='null'";


try{
Class.forName("com.ibm.db2.jcc.DB2Driver");
conn=DriverManager.getConnection("jdbc:db2://localhost:50000/mail","db2admin","db2admin");

stmt=conn.createStatement();

rs=stmt.executeQuery(Query1);
while(rs.next()){
//somethin..

String query2="select * from sony.fil2";
//again..
while(rs.next()){...}

}

 
Maneesh Godbole
Saloon Keeper
Posts: 11341
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you have an actual question?

This forum is for JSP questions. In future, while posting, please do take out time to CarefullyChooseOneForum
Moving...
 
manohar gunturu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please tell how to perform such a operation
 
Maneesh Godbole
Saloon Keeper
Posts: 11341
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having trouble understanding your problem. You have already shown us some code. What happens when you run it? Where are you stuck?
 
manohar gunturu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:I am having trouble understanding your problem. You have already shown us some code. What happens when you run it? Where are you stuck?


sir its giving error wile executing a select query inside the while loop soo...
 
Maneesh Godbole
Saloon Keeper
Posts: 11341
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since we cannot imagine what the error is, you will have to help us out here by copy pasting the stack trace
 
manohar gunturu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Since we cannot imagine what the error is, you will have to help us out here by copy pasting the stack trace


it is giving..

resultset is closed. errorcode=-4470 sqlstate=null
 
Maneesh Godbole
Saloon Keeper
Posts: 11341
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you cannot be bothered to copy paste the stack trace like I requested, I will have to take a wild guess here
You have
rs=stmt.executeQuery(Query1);
Then inside your while loop
while(rs.next()){...}
My guess is you have not defined another ResultSet instance for executing query2
Change it to
rs2 = stmt.executeQuery(Query2);
while (rs2.next())
 
manohar gunturu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Since you cannot be bothered to copy paste the stack trace like I requested, I will have to take a wild guess here
You have
rs=stmt.executeQuery(Query1);
Then inside your while loop
while(rs.next()){...}
My guess is you have not defined another ResultSet instance for executing query2
Change it to
rs2 = stmt.executeQuery(Query2);
while (rs2.next())


not like that.
i'am writing nested while(rs,next())
like

while(rs.next()){//something while(rs2.next()){..} }

 
Maneesh Godbole
Saloon Keeper
Posts: 11341
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats not what your original code says. You posted


For us to help you,
1) PostRealCode
2) Copy paste the actual stack trace
 
Tim Cooke
Sheriff
Pie
Posts: 3210
142
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're not giving us much to go on here. We can't read your mind so only know what you've posted, which isn't much.

So. Post your actual code that you've written and UseCodeTags (<--click)

Then tell us exactly what issue you are seeing when you try and run the code. Cut and Paste the Compiler / JVM error. If you get a stack trace, post that too so we can help you identify the problem.
 
manohar gunturu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:You're not giving us much to go on here. We can't read your mind so only know what you've posted, which isn't much.

So. Post your actual code that you've written and UseCodeTags (<--click)

Then tell us exactly what issue you are seeing when you try and run the code. Cut and Paste the Compiler / JVM error. If you get a stack trace, post that too so we can help you identify the problem.


ok i'am giving total code



String Query1="select * from sony.fil where private is null";

try{
Class.forName("com.ibm.db2.jcc.DB2Driver");
conn=DriverManager.getConnection("jdbc:db2://localhost:50000/mail","db2admin","db2admin");


stmt=conn.createStatement(rs.TYPE_SCROLL_SENSITIVE,
rs.CONCUR_UPDATABLE);

rs=stmt.executeQuery(Query1);
while(rs.next()){
name=rs.getString("fname");
String namef=rs.getString("name");
n=rs.getString("flname");
}


String Queryty="select * from sony.fil1 where id='"+idi+"'";
rs=stmt.executeQuery(Queryty);
while(rs.next()){
String cmnt=rs.getString("cmnt");
String cmnter=rs.getString("cmntr");
out.println(cmnt);
}
}}
catch(SQLException e) {
out.println(e); }



ERROR IS:

com.ibm.db2.jcc.am.SqlException: [jcc][t4][10120][10898][3.65.77] Invalid operation: result set is closed. ERRORCODE=-4470, SQLSTATE=null

 
manohar gunturu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Thats not what your original code says. You posted


For us to help you,
1) PostRealCode
2) Copy paste the actual stack trace


OK sir i'am giving you the error

com.ibm.db2.jcc.am.SqlException: [jcc][t4][10120][10898][3.65.77] Invalid operation: result set is closed. ERRORCODE=-4470, SQLSTATE=null
 
Tim Cooke
Sheriff
Pie
Posts: 3210
142
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your original question was asking about running a second select statement while inside a while loop processing the ResultSet of a first select statement. The code you have posted does not do this. You have two independent select statements and while loops. No nesting. You also have one too many closing braces so I suspect that you just have a copy and paste malfunction while putting your code into the post.

So lets assume that the extra closing brace that shouldn't be there is the one that looks like it's closing the first while loop. Your code would look like this? (Notice how much nicer the code looks when I UseCodeTags(<--click)).

Assuming that I've guessed right with the surplus closing brace then the advice that Maneesh gave you this morning is correct. Reusing the same ResultSet for new query results inside the while loop processing the results of a previous query is incorrect. Take his advice and declare a different ResultSet variable for the second query.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic