This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting individual values from a table

 
liverpool alan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I want to compare a string to each of the values I've got from a column. What I was doing was initialising a string array Tester[] and ,using a for loop, inserting each value from each row of the column into Tester[] for further use:
boolean val=true;
for (int i=1; val=true;i++){
try {
Tester[i]=res.getString(i);
}catch (Exception e){System.out.println(e);}
if (res.isLast()) val= false;
else val=true;
}
The "try" caught an invalid cursor state exception. Can anybody shed some light as to what is going on and how I can solve this problem??
all the best,
Alan
 
ravi janap
Ranch Hand
Posts: 389
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
boolean val=true;
for (int i=1; val=true;i++){
try {
Tester[i]=res.getString(i);
}catch (Exception e){System.out.println(e);}
if (res.isLast()) val= false;
else val=true;
}
go for val == true rather than val = true inside for loop
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "val=true" pitfall has already been mentioned. But that's not all. First, "for" idiom. What about

Personally, I find the intermediate "val" variable just confusing. Also this won't fall over on empty resultsets.
Second, the use of isLast(). Note that the Javadoc says it may be an expensive operation. Also, it does not advance the cursor - what are you trying to do? If you are trying to retrieve successive records, then you should use next() and get rid of the res.getString(i) argument:

If you're trying to get all the fields in a single result record, neither next() nor isLast() will serve you as both deal with records (rows) rather than columns. Instead:

This would need to be nested within a while(res.next()) loop or similar.
- Peter
 
liverpool alan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help guys....
I used the suggestion about next and after a little tweaking it worked nicely. Also, must include my apologies for the "val=true", that was a typing error.
All the best,
Alan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic