为什么 PreparedStatement (JDBC) 在 MySQL 5.7.9 中会导致 MySQLSyntaxErrorException?

Why does PreparedStatement (JDBC) result in a MySQLSyntaxErrorException in MySQL 5.7.9?

代码:

package com.qapro.register;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;

import java.util.ArrayList;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/RegisterMember")
public class RegisterMember extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public RegisterMember() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter("username");
        String name = request.getParameter("name");
        String surname = request.getParameter("surname");
        String email = request.getParameter("email");
        String password = request.getParameter("password");

        Connection connection = null;
        PreparedStatement preparedStatement = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");

            String connectionUrl = "jdbc:mysql://localhost:3306/qapro?user=root&password=root";
            connection = DriverManager.getConnection(connectionUrl);

            String userInsertSql = "INSERT INTO qapro.user (user_name, `password`, is_active, name, surname, email, insert_date) VALUES (?, ?, ?, ?, ?, ?, ?);";
            preparedStatement = connection.prepareStatement(userInsertSql);

            preparedStatement.setString(1, username);
            preparedStatement.setString(2, password);
            preparedStatement.setBoolean(3, true);
            preparedStatement.setString(4, name);           
            preparedStatement.setString(5, surname);
            preparedStatement.setString(6, email);
            preparedStatement.setTimestamp(7, new Timestamp(new Date().getTime()));

            int affectedRowCount = preparedStatement.executeUpdate(userInsertSql);
            if(affectedRowCount > 0)
                System.out.println("User has been inserted succesfully");
            else
                System.out.println("Error occurred while inserting user");

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        finally{
            try {
                preparedStatement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }           
    }

}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Title</title>
 </head>

 <body>
 
  <form action="RegisterMember" method="POST">
   <fieldset>
    <legend>Log in!</legend>

     Username: <input type="text" name="username"></input>
     <br></br>
     Name: <input type="text" name="name"></input>
     <br></br>
     Surname: <input type="text" name="surname"></input>
     <br></br>
     Email: <input type="email" name="email"></input>
     <br></br>
     Password: <input type="password" name="password"></input>
     <br></br>
  
     <input type="submit" value="Submit"></input>
     
   </fieldset>
  </form>
 
 </body>

</html>

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?, ?, ?)' at line 1

My database

为什么会出现此错误? 尽管发生此错误,但我的数据库正在更新。

您正在使用

int affectedRowCount = preparedStatement.executeUpdate(userInsertSql);

尝试执行查询,但这是不正确的。您已经在 .prepareStatement(userInsertSql) 调用中使用了 userInsertSql,所以您需要做的就是

int affectedRowCount = preparedStatement.executeUpdate();