我在使用注册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>
loginHandler.java
:
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){
}
}
}
rs.next()条件函数将正常工作,即如果我的数据库中已存在用户名,则它将正确地重定向到错误页面。
但是,如果它们不在表格中,那么它就不会添加它们,或者重定向到正确的页面。
所以我认为我不正确地告诉mySQL将新用户添加到我的表中。它是否正确?如果是的话,我哪里出错?
谢谢!
修改:
这些是我对我的servlet所做的更改,但仍无法让它运行起来:(
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){
}
}
}
为什么我无法成功查询mysql以将新用户添加到表中的任何提示或线索?
答案 0 :(得分:0)
在else块中,您实际上想要将用户添加到数据库,您仍然在调用第一个语句。不是你新创建的那个。这可能是造成问题的原因。
话虽这么说,您可能还想查看Hibernate框架。它可以让你自己完成所有这些工作,而不是只编写Java而不是实际的SQL。我发现让生活变得更轻松。