Solved

Java ResultSet Problem

Posted on 2011-09-22
8
2,002 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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
 
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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Your data is at risk. Probably more today that at any other time in history. There are simply more people with more access to the Web with bad intentions.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

726 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