代码:
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>
&#13;
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:您的SQL语法中有错误;查看与您的MySQL服务器版本相对应的手册,以便在“&#39;?,?,?)附近使用正确的语法”&#39;在第1行
为什么会出现此错误? 并且即使发生此错误,我的数据库也在更新。
答案 0 :(得分:0)
您正在使用
int affectedRowCount = preparedStatement.executeUpdate(userInsertSql);
尝试执行查询,但这是不正确的。您已在userInsertSql
来电中使用.prepareStatement(userInsertSql)
,因此您需要做的就是
int affectedRowCount = preparedStatement.executeUpdate();