This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes JDBC and the fly likes To compare output of result set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "To compare output of result set" Watch "To compare output of result set" New topic
Author

To compare output of result set

badri nath
Ranch Hand

Joined: Dec 21, 2005
Posts: 57
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Dec 21, 2005
Posts: 57
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Dec 21, 2005
Posts: 57
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Dec 21, 2005
Posts: 57
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: To compare output of result set
 
Similar Threads
Writing or exporting data from database to a text file in jsp
problem in servlet
ResultSet can not print the first row
How can I retrieve date using rs.getDate() ?
accessing values from servlets to jsp