Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Condition to verify if username already exists! Need your help urgentl

Hi. I am developing my registration page in JSP. I am having problems to check if username already exists, then an error massage should be displayed but my codes are not being executed. My dbase is MySQL 3.23 and web server Tomcat 3.23. Each time I get the error "Duplicate key" if username already exists.

String sql = "SELECT Username FROM user";
PreparedStatement stmt = con.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();

boolean AlreadyExists = false;
while (!rs.last() || (!AlreadyExists))
{

rs.next();
if (rs.getString("Username").compareTo("username)==0)
{
AlreadyExists = true;
//To display if username already exists
response.sendRedirect(response.encodeRedirectURL ("wrong.jsp"));
}

if (!AlreadyExists)
{


String sqlstatement = "INSERT INTO user VALUES('"+username+"','"+pwd+"','"+surname+"','"+name+"','"+category+"','"+spfield+"','"+email+"')";

PreparedStatement updsql = con.prepareStatement(sqlstatement);

updsql.executeUpdate();

//To display home page if registration is successful
response.sendRedirect(response.encodeRedirectURL("index.jsp"));
break;
}

}

Comments

  • treshrtreshr Member Posts: 326
    The way I would do it is, catching the error code Tomcat gives back to the browser and call a error page that you define in your web.xml.

    The code for defining the error page is:
    [code]

    403
    /error_403.jsp

    [/code]

    Replace the error code and location. Hope it works.


    --=][tReShR][=--

Sign In or Register to comment.