This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

ResultSet

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, I'm finishing up my login page. Users can login successfully, if they provide the wrong password they are notified, however if they enter a username that doesn't exist the servlet bombs.

My query is simple, I select all from my user table where the login ids match. So if there is no match, then obviously the loginID doesn't exist. How do I test for that though?

For example:
String query = "SELECT * from user where loginID = '"+login+"'";
ResultSet resultSet = statement.executeQuery(query);

now where do I go from here? would all the attributes I would have acquired from the query be null, and simply test to see if one if null??
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
user resultSet.next() as shown

[ March 24, 2005: Message edited by: Bear Bibeault ]
 
Jenn Person
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already know about the while(resultSet.next()) and that it means there is some results of my query. I'm asking how I can test if there are no results... like if(resultSet==null) ??? I'm new to java and need some help here!! Thanks!
 
Sripathi Krishnamurthy
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
boolean resultExists=false;
ResultSet resultSet = statement.executeQuery(query);
while(resultSet.next())
{
resultExists=true;
//if it enters here, there must be "atleast" one row that has satisfied your query.
//else if it does not enter here, there are ro rows retrieved.
}
you can use the boolean variable in your code to write further logic if required...
this is the only way as I know that you can check whether the resultset has retrieved any rows or not.

Cheers
Sripathi
[ March 24, 2005: Message edited by: Sripathi ]
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Jenn Person:
I'm asking how I can test if there are no results... like if(resultSet==null) ???



ResultSet wouldn't be null, until and unless you assign it null explicitly.
 
Ranch Hand
Posts: 301
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't you just need to do a ..

String sError = null;
String query = "SELECT * from user where loginID = '"+login+"'";
ResultSet resultSet = statement.executeQuery(query);

if (resultSet.next()) {
// check password is correct , set sError if its incorrect..

} else {
sError = "You dont exist !";
}

if (sError != null){
// go login page showing sError
} else {
// do whatever needs to be done after successfull login.
}
 
Oh, sure, you could do that. Or you could eat some pie. While reading this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!