Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

To compare output of result set

 
badri nath
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have little knowledge about Java which is troubling me.
The problem is i am retrieving values in ResultSet using
rs.getString(). Now i want to compare the value that is retrieved
with an integer value. If the compare is successful it should perform
some operation else other.
Im not able to the comparision.

................
out.println(rs.getString(1));
out.println(rs.getString(2));
.................
if(Integer.parseInt(rs.getString(1))==2002) Please help at this code
{
RequestDispatcher rd=req.getRequestDispatcher("abc.html");
rd.forward(req,res);
}else{
out.println("not executed");

}

I know the way im comparing is wrong. Hope my problem is clear.
Suggest me how to do the comparision.

The error im getting is
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index

Im just at learning stage and i use java ranch as one of my tool in learning. Thanks Ranchers in advance
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One inportant point is that each item in the result set should only be read once. It may work if you read multiple times but is not guaranteed by the specification.

So, what does the out.prinltln(rs.getString(1)); print?
 
badri nath
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, what does the out.prinltln(rs.getString(1)); print?

Hi David
Thanks for your quick reply.
It gives me the correct value that is stored in the database.
Actually im storing some Id's(int values) that will not be changed according my requirement. So im comparing the value of id's directly. Depending on the check i want to decide whether the user can view next page or not.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem that is confusing me is that the error you are getting (I found a description here does not sound like the problem you are describing. Can you even be sure it is getting to the 'if' statement?
 
badri nath
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Ya, as of my knowledge the problem is at that part of code because if i remove check using if condition then im able to go to the next page and if replace rd.forward with rds.include im able to see the output(out.println(rs.getString()) of this page also(obvious).
One thing i would like to tell you is that im including this part of code in while(rs.next()) loop. If i dont do so then im getting invalid cursor state error.
Hope i make sense
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, try these minor changes:

Firstly, change the while(rs.next()) to if(rs.next())
Secondly, read the value once and store the value. You'll get something like this:



Dave
[ April 03, 2006: Message edited by: David O'Meara ]
 
badri nath
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Its working fine within while(rs.next()) loop. The problem may be was with rs.getString(), i guess we cannot read output from rs.getString more than once. I have took that into string as suggested by you and its working nice.
Thanks for your suggestion.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic