Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Java ResultSet Problem

Posted on 2011-09-22
8
Medium Priority
?
2,202 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 668 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 668 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
LVL 4

Assisted Solution

by:stachenov
stachenov earned 664 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

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!

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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…
Suggested Courses

610 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