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

标签 java mysql sql jdbc

以下代码不会向相应的表中插入一行。 我已经测试了数据库连接并在数据库中运行了查询,这两项都通过了,但是当我通过 .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!";
}

作为引用,我的 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>

不确定我的代码哪里出错了,所以任何建议都会很有帮助。谢谢

更新:

user table in MySQL

[![抛出运行时异常后][2]][2]

最佳答案

您将 user_id 设置为 null 并且数据库提示该列不能为 null。我假设您没有将 null 传递给直接针对数据库测试的语句,因此您设置的是空字符串(或者表是设置默认值或自动生成的值,以防万一它丢失了)。

如果有默认值或自动生成的值,您只需在插入语句中保留 user_id 即可,它应该可以工作。

关于java - JDBC插入查询不更新mysql数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55110664/

相关文章:

sql - 访问另一个数据库中的sys.sql_modules

java - 使用 HashMap 时出现无法解释的行为

java - FileTranfer(DWR)中Byte[]和InputStream的区别

mysql - 更新另一个表中的多行

mysql - SQL 维护脚本

sql - PostgreSQL:额外列的性能影响

java - 跨多个 EJB 的事务

java - 为什么我收到此异常 java.lang.IllegalArgumentException : Comparison method violates its general contract

mysql - 检索内连接中表的最后一条记录

mysql - 是否可以使此查询正常工作?