Solved

Java ResultSet Problem

Posted on 2011-09-22
8
1,800 Views
Last Modified: 2012-05-12
Hi Experts...
I have a for loop which calls a function which returns a resultset object and i get the returned row for a particular iteration.
Here when called inside the for loop i am getting an exception:-java.sql.SQLException: JZ0R1: Result set is IDLE as you are not currently accessing a row.

But if i call this function outside the for loop individually passing a value it works fine.
How do i fix this.
Please help....
for(int j=0;j<remStudent.size();j++)
		 {
			 ResultSet finalList=null;
		finalList=radioDb.getFinalStudentList(remStudent.get(j));
		finalList.next();
		System.out.println(finalList.getString(1)+" "+finalList.getInt(2));
		thirdMap.put(finalList.getString(1),finalList.getInt(2));
		 }

Open in new window

0
Comment
Question by:gaugeta
8 Comments
 
LVL 4

Accepted Solution

by:
nfaria earned 167 total points
ID: 36581413
Do you always have results?

You should have

if (finalList.next()) {
   System.out.println(finalList.getString(1)+" "+finalList.getInt(2));
   thirdMap.put(finalList.getString(1),finalList.getInt(2));
}

To optimize you should also have this

ResultSet finalList=null;

Outside the for as your radioDb.getFinalStudentList(remStudent.get(j)); will return something != null
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36581662
I don't think with result set you can call say
finalList.getString(1) twice if you do not call  finalList.next();
in between, like you do:

	finalList.next();
		System.out.println(finalList.getString(1)+" "+finalList.getInt(2));
		thirdMap.put(finalList.getString(1),finalList.getInt(2));

Open in new window


0
 
LVL 47

Assisted Solution

by:for_yan
for_yan earned 167 total points
ID: 36581674
You should rather do something like that:

	finalList.next();
       String s = finalList.getString(1);
         int i = finalList.getInt(2);
		System.out.println(s + " " + i);
		thirdMap.put(s,i);

Open in new window


0
 
LVL 4

Assisted Solution

by:stachenov
stachenov earned 166 total points
ID: 36582119
In addition to what for_yan said, it is also not always possible to retrieve fields in any order. For example, this will break for some DB drivers:
int i = finalList.getInt(2);
String s = finalList.getString(1);

Open in new window

So the complete rule is: retrieve every field only once per row, and retrieve all the fields for the row in the order they have been selected.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 86

Expert Comment

by:CEHJ
ID: 36582163
The code you posted looks very dubious. You are repeatedly trying to obtain a ResultSet in a loop, where you should probably be retrieving a row from a one and only ResultSet
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36582184
... and the very first comment in this thread shows you the general pattern for retrieving results from a ResultSet
0
 
LVL 1

Expert Comment

by:rajeeshb
ID: 36582884
Looks like there is no data available for some queries. Check that finalList.next() is set to "true" before calling get methods on finalList.

Try this :-

for(int j=0;j<remStudent.size();j++)
{
    ResultSet finalList=null;
    finalList=radioDb.getFinalStudentList(remStudent.get(j));
    if (finalList.next()) {
        System.out.println(finalList.getString(1)+" "+finalList.getInt(2));
        thirdMap.put(finalList.getString(1),finalList.getInt(2));
    }
    else {
        System.out.println("No data available for : " + remStudent.get(j) );
    }
}
0
 
LVL 4

Expert Comment

by:reijnemans
ID: 36585878
Hi augeta,

Because this is not the first topic where you get a "sybase-driver" exception, please read this page http://www.sybase.com/detail?id=1009784. There is an action specified for every exception you could get. As for this problem the first comment on this post is the answer to your problem

Regards,

Reijnemans
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now