简体   繁体   English

JDBC插入查询不会更新mysql数据库

[英]JDBC insert query does not update mysql database

The following code does not insert a row into the corresponding table. 以下代码不会在相应的表中插入行。 I have already tested the db connection and ran the query in the database, both of which have passed however when I add inputs via a .jsp form the values are still not inserting. 我已经测试了数据库连接,并在数据库中运行了查询,但是当我通过.jsp表单添加输入时,值仍未插入,但这两个查询都已通过。

public class UserDao {

public String registerUser(User user){
    String username = user.getUsername();
    String email = user.getEmail();
    String password = user.getPassword();

    Connection con;
    con = DBConnection.createConnection();

    PreparedStatement preparedStatement;

    try{
        con.setAutoCommit(false);

        String query = ("INSERT INTO user (username, email, password, user_id) VALUES(?, ?, ?, ?)");
        preparedStatement = con.prepareStatement(query);
        preparedStatement.setString(1, username);
        preparedStatement.setString(2, email);
        preparedStatement.setString(3, password);
        preparedStatement.setString(4,null);

        int i = preparedStatement.executeUpdate();
        con.commit();
        preparedStatement.close();
        con.close();

        if(i !=0 ){
            return "SUCCESS";
        }
    }catch(SQLException e){
      throw new RuntimeException(e);

    }

    return "Something is wrong!";
}

} }

For reference here is what my servlet class and .jsp file looks also like: 这里参考的是我的servlet类和.jsp文件看起来像:

public class UserRegistrationServlet extends HttpServlet {

public UserRegistrationServlet(){}
/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String userName = request.getParameter("username");
    String email = request.getParameter("email");
    String password = request.getParameter("password");
    User user = new User();
    user.setUsername(userName);
    user.setEmail(email);
    user.setPassword(password);
    UserDao userDao = new UserDao();
    String userRegistered = userDao.registerUser(user);
    if(userRegistered.equals("SUCCESS")){
        request.getRequestDispatcher("test.jsp").forward(request, response);

    }else{
        request.setAttribute("error", userRegistered);
        request.getRequestDispatcher("/UserRegistration.jsp").forward(request, response);
    }
}

} }

    <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Register</title>
<script> 
function validate()
{ 
var username = document.form.username.value; 
var email = document.form.email.value;
var password = document.form.password.value;
var conpassword= document.form.conpassword.value;
if (username==null || username=="")
{ 
alert("Full Name can't be blank"); 
return false; 
}
else if (email==null || email=="")
{ 
alert("Email can't be blank"); 
return false; 
}
else if(password.length<6)
{ 
alert("Password must be at least 6 characters long."); 
return false; 
} 
else if (password!=conpassword)
{ 
alert("Confirm Password should match with the Password"); 
return false; 
} 
} 
</script> 
</head>
<body>
<center><h2>Java Registration application using MVC and MySQL </h2></center>
<form name="form" action="UserRegistrationServlet" method="post" onsubmit="return validate()">
<table align="center">
<tr>
<td>Username</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td>Password</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td>Confirm Password</td>
<td><input type="password" name="conpassword" /></td>
</tr>
<tr>
<td><%=(request.getAttribute("errMessage") == null) ? ""
: request.getAttribute("errMessage")%></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Register"></input><input
type="reset" value="Reset"></input></td>
</tr>
</table>
</form>
</body>
</html>

Not sure where the error in my code, so any advice will be very helpful. 不知道我的代码中的错误在哪里,所以任何建议都会非常有帮助。 Thanks 谢谢

Updates: 更新:

MySQL中的用户表

[![After throwing a runtime exception ][2]][2] [![抛出运行时异常后] [2]] [2]

You're setting user_id to null and the database complains that the column must not be null . 您将user_id设置为null ,并且数据库抱怨该列不能为null I assume that you haven't passed null to the statement you tested against the DB directly, so that you were setting an empty string instead (or the table is to set a default or automatically generated value in case it's missing). 我假设您没有直接将null传递给您针对数据库测试的语句,因此您设置了一个空字符串(或者表是设置默认值或自动生成的值,以防它丢失)。

If there is a default value or a autogenerated one, you can simply leave away user_id in your insert statement and it should work. 如果有默认值或自动生成的值,您可以在insert语句中放弃user_id ,它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM