check / handle username unavailability(already in use as primary key is username) error in Dynamic Web Project

Viewed 116

I am developing the project for college, I am new to this interface and need help for this concern. I have created a DB on the mysql using SQLyog and Dynamic web project on eclipse. For the registration I have used the following code:

register.jsp

<form action="register1.jsp" method="post">
                    <div class="firstname">
                        <label for="firstname"></label> <input type="text"
                            placeholder="My first name is" name="firstname" required>
                    </div>
                    <div class="lastname">
                        <label for="lastname"></label> <input type="text"
                            placeholder="My last name is" name="lastname" required>
                    </div>
                    <div class="email">
                        <label for="email"></label> <input type="email"
                            placeholder="My e-mail ID is" name="email_id" required>
                    </div>
                    <div class="username">
                        <label for="username"></label> <input type="text"
                            placeholder="My username is" name="username" required>
                    </div>
                    <div class="pwd">
                        <label for="pwd"></label> <input type="password"
                            placeholder="My password is" name="pwd" required>
                    </div>
                    <div class="submit">
                        <input type="submit" value="Register" id="form_button" />
                    </div>
                </form>

register1.jsp


<%@ page import="java.sql.*"%>
<%@ page import="javax.sql.*"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%
String firstname=request.getParameter("firstname");
String lastname=request.getParameter("lastname");
String username=request.getParameter("username");
session.putValue("username",username);
String pwd=request.getParameter("pwd");
String email_id=request.getParameter("email_id");

Class.forName("com.mysql.jdbc.Driver");
Connection con= DriverManager.getConnection("jdbc:mysql://localhost/cinushi_university","CinthiyaSingh", "Cinthiya@098");
Statement st=con.createStatement();
ResultSet rs;
int i=st.executeUpdate("insert into users values('"+ username+ "','"+pwd+ "','"+firstname+ "','"+ lastname+ "','"+ email_id+ "')");
if (i > 0) {
    response.sendRedirect("login.jsp");
} else {
    request.setAttribute("errorMessage", "Already Existing Username/Email");
    request.getRequestDispatcher("Register.jsp").forward(request, response);

}%>

I want the registration page to reload when the query tries to violate the primary key (username) property and display the error of :"username already in use". However, the error page of the server is loaded with the error details.

Please look into this. I don't want to use any other file preferably.

1 Answers

Use select statement first to check that username already exist in your table , then if exist show error and redirect else insert data in table.So your code will look like below :

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection("jdbc:mysql://localhost/cinushi_university", "CinthiyaSingh", "Cinthiya@098");
    //selecting username from users table
    PreparedStatement ps = con.prepareStatement("select *from users where username=?");
    ps.setString(1, username);
    ResultSet rs = ps.executeQuery();
    //if true
    if (rs.next()) {
     //sending error message on Register page
     request.setAttribute("errorMessage", "Already Existing Username/Email");
     request.getRequestDispatcher("Register.jsp").forward(request, response);

    } else {
     //If not exist performing insert 
     PreparedStatement ps1 = con.prepareStatement("insert into users values(?,?,?,?,?)");
     //setting value for "?"
     ps1.setString(1, username);
     ps1.setString(2, pwd);
     ps1.setString(3, firstname);
     ps1.setString(4, lastname);
     ps1.setString(5, email_id);
     int i = ps1.executeUpdate();
     //if updated successfully
     if (i > 0) {
      //redirect
      response.sendRedirect("login.jsp");
     }
   //close connection
    }

Then get above error message in Register.jsp using ${errorMessage} .

Related