下面的代碼不會在相應的表中插入一行。我已經測試了數據庫連接并在數據庫中運行了查詢,但是當我通過.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 */@Overrideprotected 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();不確定我的代碼中的錯誤在哪里,所以任何建議都會非常有幫助。謝謝
JDBC 插入查詢不更新 mysql 數據庫
慕工程0101907
2022-08-03 10:54:40