Java - 如何检查数据库中的现有用户(对于注册接口)?

时间:2014-04-09 11:09:26

标签: java mysql servlets dao

所以这就是我迄今为止所做的。它也已经以用户名作为主键连接到数据库。

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{

    }
}

我想要的是,当我尝试使用数据库中任何现有用户的相同用户名/电子邮件注册帐户时,它将使用response.sendRedireect("文件)重定向到另一个页面。 JSP&#34);这将要求用户再次使用其他用户名/电子邮件注册。提前谢谢!

这是我的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);
    }
}

1 个答案:

答案 0 :(得分:1)

一旦您将username作为主要内容,就不允许插入重复项。所以试试这个

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

如果它是重复的,它将抛出异常ora-,所以使用catch块使用页面重定向处理异常。

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

<强>更新

声明integer变量以检查是否已插入值。同时将方法addAccount(Accounts bean)的返回类型更改为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;
}

在您的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);
}

希望这有帮助!