jsp--if condition
<%
while(resultset.next()){
for (int i=1; i<=1; i++){
String re =resultset.getString(i);
if(username.equals(re) ){
out.write("That username already exists.<br>Enter another.");
}
elseif(username != re){
out.write("Hi new user");
}
}
}
%>
Hi,
I have this table with a column in a database, where the are stored usernames, each row with a different username.
in this code the variable "username" represents an input from keyboard that a user/person enters.
i check every row of the column with a "for",
If the person enters a username that is not in the database, it prints "Hi new user"
The problem I have is that when a person enters a username that exists in the database the program is printing the two messages:
That username already exists
and
Hi new user
how can i solve this?
please help me guys
thanks.
[1437 byte] By [
deroka] at [2007-11-27 8:48:07]

# 1
Well there are two ways to approach this.
1 - Your query is pulling back all the names in the database. You can't say if a person is present or not until you have looked at all the names.
Using a boolean value to record if you have "found" the name or not would work.
You could also break out of the loop when you find the name (save looking at the rest of the list)
Also note that to check if strings are not equal, you should use !username.equals(re)
2 - A better approach might be to let your database tell you if the name is there or not: "Select * from userTable where username = ?"
Assuming username is unique, that would either return 1 row (is there) or 0 rows(not there). No looping required.
# 4
"2 - A better approach might be to let your database tell you if the name is there or not: "Select * from userTable where username = ?"
Assuming username is unique, that would either return 1 row (is there) or 0 rows(not there). No looping required."
In which case you should simply check by using an if-statement IF there is a resultset, which prevents JSP from throwing an SQL-Exception, as far as I know.