문제

So here's what I've done so far. it is also already connected to the database with the username as the primary key.

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try {
        Accounts bean = new Accounts();
        bean.setUsername(request.getParameter("uname"));
        bean.setName(request.getParameter("fname"));
        bean.setEmail(request.getParameter("email"));
        bean.setPassword(request.getParameter("password"));
        AccountsDAO aO = new AccountsDAO();
        aO.addAccount(bean);
        response.sendRedirect("Stream.jsp");
    }
    finally{

    }
}

what i want is, when i try to register an account with the same username/email of any existing user in the database, it will redirect to another with another page also using a response.sendRedireect("file.jsp"); that will require the user to register again with another username/email. Thank you in advance!

here is my AccountsDAO

public void addAccount(Accounts bean) {
    try {
        bConnector = new DBConnector();
        Connection c = bConnector.getConnection();
        String query = "insert into account (username, name, email, password) values (?,?,?,?)";
        PreparedStatement preparedstatement = c.prepareStatement(query);
        preparedstatement.setString(1, bean.getUsername());
        preparedstatement.setString(2, bean.getName());
        preparedstatement.setString(3, bean.getEmail());
        preparedstatement.setString(4, bean.getPassword());
        preparedstatement.executeUpdate();
    } catch (SQLException ex) {
        Logger.getLogger(AccountsDAO.class.getName()).log(Level.SEVERE, null, ex);
    }
}
도움이 되었습니까?

해결책

Once you have username as primary it will not allow to insert duplicates . so try like this

try
{
//query to insert your username which you get from user
}

If it is duplicate it will throw an exception as ora- so handle the exception using page redirect using catch block.

catch(Exception e)
{
system.out.print(e);
response.sendRedirect("file.jsp");
}

Update

Declare an integer variable to check whether you have inserted the value . Also change the return type of the method addAccount(Accounts bean) to int

public int addAccount(Accounts bean) {

    int count=0; 
    try {
        bConnector = new DBConnector();
        Connection c = bConnector.getConnection();
        String query = "insert into account (username, name, email, password) values (?,?,?,?)";
        PreparedStatement preparedstatement = c.prepareStatement(query);
        preparedstatement.setString(1, bean.getUsername());
        preparedstatement.setString(2, bean.getName());
        preparedstatement.setString(3, bean.getEmail());
        preparedstatement.setString(4, bean.getPassword());
        count=preparedstatement.executeUpdate();
    } catch (SQLException ex) {
        Logger.getLogger(AccountsDAO.class.getName()).log(Level.SEVERE, null, ex);
    }
 return count;
}

In your servlet,

int count=0;
try {
        Accounts bean = new Accounts();
        bean.setUsername(request.getParameter("uname"));
        bean.setName(request.getParameter("fname"));
        bean.setEmail(request.getParameter("email"));
        bean.setPassword(request.getParameter("password"));
        AccountsDAO aO = new AccountsDAO();
        count=aO.addAccount(bean);
        if(count>0){
        response.sendRedirect("Stream.jsp"); \\Success condition here
        }
        else{
         response.sendRedirect("file.jsp");   \\ failure condition here
       }
    }
    catch(Exception e)
{
system.out.print(e);
}

Hope this helps!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top