This question already has answers here:
Cannot issue data manipulation statements with executeQuery()
                                
                                    (9个答案)
                                
                        
                                3年前关闭。
            
                    
我的数据库表未插入此信息,并且我看不到错误在哪里,以及插入是否成功。请帮忙!

        try{

             Class.forName("com.mysql.jdbc.Driver").newInstance();
             Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/payroll_system", "root", "");
             PreparedStatement ps = con.prepareStatement("INSERT INTO payroll_system.employee_info(employeeID, FirstName, LastName, Admin,DOB,Address,Email,HourlyRate,Gender,ALeaveBalance,SLeaveBalance,ActiveStatus,Role,BSB,BankName,AccNumber,SuperNumber,SuperCompany) values(?,?,?,?,?,?,?,?,?,?,?,1,?,?,?,?,?,?)");
             ps.setString(1, employee_id);
             ps.setString(2, firstName);
             ps.setString(3, lastName);
             ps.setString(4, admin);
             ps.setString(5, DOB);
             ps.setString(6, address);
             ps.setString(7, email);
             ps.setString(8, HPR);
             ps.setString(9, gender);
             ps.setString(10, ALB);
             ps.setString(11, SLB);
             ps.setString(12, Role);
             ps.setString(13, BSB);
             ps.setString(14, BankName);
             ps.setString(15, BAN);
             ps.setString(16, SAC);
             ps.setString(17, SAN);
             ResultSet rs = ps.executeQuery();
             st=rs.next();
             if(st) {
                 out.println("Account successfully created!");
                 RequestDispatcher rd = request.getRequestDispatcher("index.html");
                 rd.include(request, response);
             }
             else{
                 RequestDispatcher rd = request.getRequestDispatcher("changePassAdmin.html");
                 rd.include(request, response);
             }
         }catch(Exception e)
        {
            e.printStackTrace();
        }
      out.close();

最佳答案

executeQuery仅用于查询,您需要编写

 ps.executeUpdate();

09-26 02:13