Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How to verify if user already exists in MySQL database using JSP?

843838Jan 15 2006 — edited Jan 17 2006
Hi, I am trying to create a website that allows users to register and then login. All regsitration details are stored in a MySQL table and I am attempting to have a java bean check that a new user does not already exist by looking up the username. However, I cannot get this to work, the bean is allowing anyone through. I believe there may be a problem with my while loop as the bean checks through the stored usernames in the database. Any suggestions appreciated thanks. See my code below:[

code]package foo;
import java.sql.*;
import java.util.*;
public class FormBean {
private String firstName;
private String lastName;
private String email;
private String userName;
private String password1;
private String password2;
private Hashtable errors;

public boolean validate() {
boolean allOk=true;
if (firstName.equals("")) {
errors.put("firstName","Please enter your first name");
firstName="";
allOk=false;
}
if (lastName.equals("")) {
errors.put("lastName","Please enter your last name");
lastName="";
allOk=false;
}
if (email.equals("") || (email.indexOf('@') == -1)) {
errors.put("email","Please enter a valid email address");
email="";
allOk=false;
}
if (userName.equals("")) {
errors.put("userName","Please enter a username");
userName="";
allOk=false;
}
try {
String database = "******hidden******";
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(database,"********hidden*******");
Statement stat = conn.createStatement();
String query="SELECT u_name FROM users;";
String DbUserName="";
ResultSet rst=stat.executeQuery(query);
while(rst.next()) {
DbUserName=rst.getString("u_name");
if (userName.equals(DbUserName)) {
allOk=false;
errors.put("userName","User name already exists, please choose another");
userName="";
conn.close();
break;
}
}

} catch (Exception ex) {
ex.printStackTrace();
}
if (password1.equals("") ) {
errors.put("password1","Please enter a valid password");
password1="";
allOk=false;
}
if (!password1.equals("") && (password2.equals("") || !password1.equals(password2))) {
errors.put("password2","Please confirm your password");
password2="";
allOk=false;
}

return allOk;

}
public String getErrorMsg(String s) {
String errorMsg =(String)errors.get(s.trim());
return (errorMsg == null) ? "":errorMsg;
}

public FormBean() {
firstName="";
lastName="";
email="";
userName="";
password1="";
password2="";
errors = new Hashtable();
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

public String getEmail() {
return email;
}

public String getUserName() {
return userName;
}

public String getPassword1() {
return password1;
}

public String getPassword2() {
return password2;
}

public void setFirstName(String fname) {
firstName =fname;
}

public void setLastName(String lname) {
lastName =lname;
}

public void setEmail(String eml) {
email=eml;
}

public void setUserName(String u) {
userName=u;
}

public void setPassword1(String p1) {
password1=p1;
}

public void setPassword2(String p2) {
password2=p2;
}

public void setErrors(String key, String msg) {
errors.put(key,msg);
}

}



Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 14 2006
Added on Jan 15 2006
10 comments
4,627 views