Solved

Java ResultSet Problem

Posted on 2011-09-22
8
1,934 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Creating and Managing Databases with phpMyAdmin in cPanel.
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

730 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