以下代码不会在相应的表中插入一行。 我已经测试了数据库连接并在数据库中运行了查询,两者均已通过,但是当我通过.jsp表单添加输入时,值仍未插入。
<script type="text/javascript">
var myArray =
['<a href="https://ceiti.md/">CEITI</a>',
['<a href="#">Inner Value 1</a>',
'<a href="#">Inner Value 2</a>',
'<a href="#">Inner Value 3</a>',
'<a href="#">Inner Value 4</a>'],
'<a href="#">Value 2</a>',
'<a href="#">Value 3</a>',
'<a href="#">Value 4</a>',
'<a href="#">Value 5</a>',
'<a href="#">Value 6</a>'];
function iter(target) {
var ul = document.createElement('ul'),
li;
ul.setAttribute('class','lifin');
target.appendChild(ul);
return function (a) {
if (Array.isArray(a)) {
if (!li) {
li = document.createElement('li');
ul.appendChild(li);
}
a.forEach(iter(li));
ul.setAttribute('id','proList');
li.setAttribute('id','pList');
return;
}
li = document.createElement('li');
li.setAttribute('id','Mon');
li.appendChild(document.createTextNode(a));
ul.appendChild(li);
};
}
myArray.forEach(iter(document.getElementById('myList')));
</script>
}
作为参考,这是我的servlet类和.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!";
}
}
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);
}
}
不确定我的代码中的错误在哪里,因此任何建议都将非常有帮助。谢谢
更新:
[![引发运行时异常之后] [2]] [2]
答案 0 :(得分:3)
您正在将user_id
设置为null
,数据库抱怨该列不能为null
。我假设您没有直接将null
传递给针对DB进行测试的语句,因此您正在设置一个空字符串(或者该表将设置默认值或自动生成的值,以防丢失)
如果有默认值或自动生成的值,则只需在插入语句中省略user_id
,它就可以工作。