For some reason the login only works for the last user in the database. I have a while loop, but I think it makes the program to go to the last user. I tried using if statement but then only the first user can log in.
if (username!=null && password!=null) {
pagename = "main";
} else {
username = request.getParameter("username");
password = request.getParameter("password");
while(results.next())
{
if(results.getString(2).equals(password) && results.getString(1).equals(username))
{
pagename="main";
}
else
{
pagename="start";
}
}
}
How is this caused and how can I solve it?
You are copying the entire DB table into Java’s memory and doing the comparison in a
whileloop over all records. You are not aborting thewhileloop when there’s a match with a record, so it continues looping over the remaining records and so thepagenameget overridden with “start” everytime.You need to add a
breakstatement:Or, better, let SQL do the job it is designed for, selecting and returning exactly the data you need:
That’s more efficient and sensible.