This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JDBC and the fly likes ResultSet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "ResultSet" Watch "ResultSet" New topic


Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Hi. If a user keys in a postal code that is not in the table, I want an error message to come back stating this. My problem is that I can't seem to figure out how to tell if my select statement didn't find anything. Is there a method I can use that is sort of like a getRowCount? I've tried comparing to null, but that doesn't work. I've also tried comparing the row to zero, but that doesn't get the job done either.
Any help is appreciated!!
Thanks in advance!
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
If all you want to know is if the number they enter is in the table then try this:
SELECT count(*) FROM table WHERE zipcodeColumn = 'userValue'
That should just return an integer in the count field that you can check to see if it greater than zero. Either way you do it you should be getting a ResultSet back then just use next to see if there is a row or not. But I know you know that so I might not be understanding your question correctly.
Another option would be to turn it into an update and not really do anything in the update:
UPDATE table SET zipcodeColumn = 'userValue' WHERE zipcodeColumn = 'userValue'
That should still return an update number even if it really doesn't do anything - but that might be kind of dangerous too if the user does something strange.
Hope that helps a little bit

Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 60789

Or, if you want to just check this out using your existing SQL and result set, you could just do something like:


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kareem Qureshi
Ranch Hand

Joined: Mar 14, 2002
Posts: 102
If i were u and understood ur problem(U want to check whether the entered number which is a zipcode is valid or not) use the zipcode as a type table and cache it then i dont have to go to database everytime to check it, if it does exist and then only goahead and do rest of the processing this way i will not be tying up the precious system resources.
Sorry for not giving a quick answer but for quick fix Bill suggestion is good
Kareem Qureshi
Ranch Hand

Joined: Mar 14, 2002
Posts: 102
I meant Bear B not bill
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Thanks for the help guys!
I appreciate it!
wood burning stoves
subject: ResultSet
Similar Threads
what will happen while adding already existing panel in JTabbedPane?
java.sql.SQLException: invalid name pattern
Concatenate all of the strings in an array
Session beans, WSAD, and Servants
Question about inserting elements into a treemap as compared to arraylist?