【问题标题】:JDBC insert query does not update mysql databaseJDBC插入查询不更新mysql数据库
【发布时间】:2019-03-11 21:26:42
【问题描述】:

下面的代码不会在对应的表中插入一行。 我已经测试了 db 连接并在数据库中运行了查询,但两者都通过了,但是当我通过 .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>

不确定我的代码中的错误在哪里,所以任何建议都会非常有帮助。谢谢

更新:

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

【问题讨论】:

  • 请更新问题中的表格描述。另外,使用您在控制台上看到的任何错误日志进行更新?
  • 您好,我已经添加了您请求的更新(第一行来自我之前测试查询在数据库中是否有效)。我也没有在控制台上看到任何错误日志。
  • 一般来说,捕获异常而不对它们做任何事情并不是一个好习惯。当您捕获“SQLException”时,您能否抛出一个新的“RuntimeException”而不是仅打印堆栈跟踪?这可能不是您的问题,但我已经看到与此类似的代码隐藏了明显的问题。
  • @CharlieWallace 您好,我按照您的要求进行了更改,现在看到了错误。我需要对 id 列进行哪些更改才能自动递增?
  • 该错误告诉您将空值传递给配置为需要值的列。您的代码说您正在向该列传递一个空值,user_id。那么你觉得神秘的是什么?

标签: java mysql sql jdbc


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多