Solved

Java ResultSet Problem

Posted on 2011-09-22
8
1,872 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ODBC Question 3 42
throw exception 21 44
Java Restore security prompts not working 10 13
Is there a simpler dropbox system? 10 22
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…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

809 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