Java 使用MySQL登录/注册Servlet

Java 使用MySQL登录/注册Servlet,java,mysql,servlets,registration,Java,Mysql,Servlets,Registration,我在让注册servlet向mySQL表添加行时遇到问题。这就是说,servlet可以看到用户是否已经在我的表中,并将相应地重定向(因此我知道我的连接在那里)。我还测试了我的查询字符串,它似乎是正确的。我不确定我做错了什么,也不确定我如何准确地看到我的mySQL服务器看到了什么消息/它如何响应注册请求 这是我的代码: index.jsp <div id="loginDiv"> <form action="

我在让注册servlet向mySQL表添加行时遇到问题。这就是说,servlet可以看到用户是否已经在我的表中,并将相应地重定向(因此我知道我的连接在那里)。我还测试了我的查询字符串,它似乎是正确的。我不确定我做错了什么,也不确定我如何准确地看到我的mySQL服务器看到了什么消息/它如何响应注册请求

这是我的代码:

index.jsp

                    <div id="loginDiv">

                <form action="LoginHandler" method="post" id="1">
                Login Here! -->
                    Username: <input type="text" name="username" required="required"> </input>
                    Password: <input type="password" name="password" required="required"> </input>
                <input type="submit" value="Login"> </input> 
                </form>
                </div>

                <div id="registerDiv">
                <form action="RegistrationHandler" method="post" id="2">
                Register Here! -->
                    Username: <input type="text" name="username" required="required"> </input>
                    Password: <input type="password" name="password" required="required"> </input>
                <input type="submit" value="Register"> </input>
                </form>
                </div>
rs.next()条件函数将正常工作,即如果我的数据库中已有用户名的用户,它将正确重定向到错误页面

但是,如果它们不在表中,则不会添加它们,也不会重定向到正确的页面

因此,我认为我不恰当地告诉mySQL向我的表中添加一个新用户。这是正确的吗?如果是这样,我错在哪里了

谢谢大家!

编辑

以下是我对servlet所做的更改,但仍然没有成功地使其正常工作:(


关于为什么我无法成功查询mysql以将此新用户添加到表中的任何提示或线索?

在else块中,您实际上希望将用户添加到数据库中,您仍在调用第一条语句,而不是您新创建的语句。这可能是导致问题的原因


话虽如此,您可能还想看看Hibernate框架。它可以让您不用自己做这些事情,而只需编写Java代码,而不是实际的SQL。我发现这使生活变得更加轻松。

这个rs=st.executeQuery中不处理查询吗(createUserQuery);?OHHH>我现在明白了,谢谢你纠正了这个问题,但还是没有成功。好的,你试过放一个
e.printStackTrace()吗
在您的catch语句中?这会打印出一条stacktrace,其中包含一条关于实际出错原因的消息。这可能会对这件事有所帮助。再次感谢您Ractoc!我不知道printstacktrace。将其添加到我的异常中后,我在tomcat中发现了此错误消息“java.sql.SQLException:无法使用executeQuery()发出数据操作语句。”这将我引向另一个stackoverflow线程,在将executeQuery()更改为executeUpdate()后,错误被解决了!万岁!感谢您教我有关stackTrace的知识,这对未来将是一个巨大的帮助!您就是那个人!
public class RegistrationHandler extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    //get username & pass from jsp login
        String userName = request.getParameter("username");
        String passWord = request.getParameter("password");
    //pass username & pass to mysql query
    Connection conn = null;
    String url = "jdbc:mysql://localhost:8888/";
    String dataBase = "usersdb";
    String driver = "com.mysql.jdbc.Driver";
    String dBUserName = "admin";
    String dBPassword = "xxxx";
    RequestDispatcher rd = null;

    try{

        Class.forName(driver).newInstance();
        conn = (Connection) DriverManager.getConnection(url+dataBase,dBUserName,dBPassword);
        String strQuery = "select * from playerinfo where usrname='" + userName + "'" + ";" ;
        Statement st = (Statement) conn.createStatement();
        ResultSet rs = st.executeQuery(strQuery);

        if(rs.next())
        //username already exists
        {
        request.setAttribute("userName", userName);
        RequestDispatcher requestDispatcher = request
            .getRequestDispatcher("/registrationError.jsp");
        requestDispatcher.forward(request,response);
        }

        else
        //create new user in database
        {
        String createUserQuery = "insert into playerinfo values ('" + userName + "','" + passWord + "'," + 100 + "," + 100 + "," + 100 + "," + "NULL" + "," + "NULL" + ") ;" ;
        Statement create = (Statement) conn.createStatement();
        rs = st.executeQuery(createUserQuery);

        RequestDispatcher requestDispatcher = request
            .getRequestDispatcher("/userHome.jsp");
        requestDispatcher.forward(request,response);
        }
    //close connection
    rs.close();
    st.close();
    }
    catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e){
    }
}

}
public class RegistrationHandler extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    //get username & pass from jsp login
        String userName = request.getParameter("username");
        String passWord = request.getParameter("password");
    //pass username & pass to mysql query
    Connection conn = null;
    Connection conn2 = null;
    String url = "jdbc:mysql://localhost:8888/";
    String dataBase = "usersdb";
    String driver = "com.mysql.jdbc.Driver";
    String dBUserName = "admin";
    String dBPassword = "xxxx";
    RequestDispatcher rd = null;

    try{

        Class.forName(driver).newInstance();
        conn = (Connection) DriverManager.getConnection(url+dataBase,dBUserName,dBPassword);
        String strQuery = "select * from playerinfo where usrname='" + userName + "'"  ;
        Statement st = (Statement) conn.createStatement();
        ResultSet rs = st.executeQuery(strQuery);

        if(rs.next())
        //username already exists
        {
        request.setAttribute("userName", userName);
        RequestDispatcher requestDispatcher = request
            .getRequestDispatcher("/registrationError.jsp");
        requestDispatcher.forward(request,response);
        }

        else
        //create new user in database
        {
        register(userName, passWord);

        RequestDispatcher requestDispatcher = request
            .getRequestDispatcher("/userHome.jsp");
        requestDispatcher.forward(request,response);
        }
    //close connection
    rs.close();
    st.close();
    }
    catch (ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e){
    }



        }
private static void register(String username, String password){
    Connection conn = null;
    String url = "jdbc:mysql://localhost:8888/";
    String dataBase = "usersdb";
    String driver = "com.mysql.jdbc.Driver";
    String dBUserName = "admin";
    String dBPassword = "xxxx";

    try{

        Class.forName(driver).newInstance();
        conn = (Connection) DriverManager.getConnection(url+dataBase,dBUserName,dBPassword);
        String strQuery = "insert into playerinfo values ('" + username + "','" + password + "'," + 100 + "," + 100 + "," + 100 + "," + "NULL" + "," + "NULL" + ") " ;
        Statement st = (Statement) conn.createStatement();
        st.executeQuery(strQuery);

    }
    catch(ClassNotFoundException | InstantiationException | IllegalAccessException | SQLException e){

    }
  }
}