Duplicate entry for primary key – registration form

huangapple go评论157阅读模式
英文:

Duplicate entry for primary key - registration form

问题

我有一个简单的JSP、Servlet和MySQL注册表单。但是,我只能注册一个用户,然后出现以下错误:

严重:消息:键'PRIMARY'的重复条目'1'。

当然,当我从数据库中删除该用户后,我可以再次创建,但只能创建一个用户。此外,我在该列上设置了自动递增:

CREATE TABLE user (id int(3) NOT NULL AUTO_INCREMENT ...

这也是我的数据库插入方法:

  1. public class UserDAO {
  2. public int registerUser(User User) throws ClassNotFoundException, SQLException {
  3. String INSERT_USERS_SQL = "INSERT INTO user"
  4. + " (id, firstName, lastName, username, password, email) VALUES "
  5. + " (?, ?, ?, ?, ?, ?)";
  6. int result = 0;
  7. Class.forName("com.mysql.jdbc.Driver");
  8. try (Connection connection = DriverManager
  9. .getConnection("jdbc:mysql://localhost:3306/cs230projekat","root","");
  10. // Step 2:Create a statement using connection object
  11. PreparedStatement preparedStatement = connection.prepareStatement(INSERT_USERS_SQL)) {
  12. preparedStatement.setInt(1, 1);
  13. preparedStatement.setString(2, User.getFirstName());
  14. preparedStatement.setString(3, User.getLastName());
  15. preparedStatement.setString(4, User.getUsername());
  16. preparedStatement.setString(5, User.getPassword());
  17. preparedStatement.setString(6, User.getEmail());
  18. System.out.println(preparedStatement);
  19. // Step 3: Execute the query or update query
  20. result = preparedStatement.executeUpdate();
  21. } catch (SQLException e) {
  22. // process sql exception
  23. printSQLException(e);
  24. }
  25. return result;
  26. }
  27. }

谢谢。

英文:

I have one simple registraton form with jsp, servlet and mysql. But, I can register only one user, then i got this error:

Severe: Message: Duplicate entry '1' for key 'PRIMARY'. Of course, when I delete that user from database I can create again but just one user. Also, I put auto increment on that column:

CREATE TABLE user (id int(3) NOT NULL AUTO_INCREMENT ...

This is also my method for insert in db:

  1. public class UserDAO {
  2. public int registerUser(User User) throws ClassNotFoundException, SQLException {
  3. String INSERT_USERS_SQL = "INSERT INTO user"
  4. + " (id, firstName, lastName, username, password, email) VALUES "
  5. + " (?, ?, ?, ?, ?, ?)";
  6. int result = 0;
  7. Class.forName("com.mysql.jdbc.Driver");
  8. try (Connection connection = DriverManager
  9. .getConnection("jdbc:mysql://localhost:3306/cs230projekat","root","");
  10. // Step 2:Create a statement using connection object
  11. PreparedStatement preparedStatement = connection.prepareStatement(INSERT_USERS_SQL)) {
  12. preparedStatement.setInt(1, 1);
  13. preparedStatement.setString(2, User.getFirstName());
  14. preparedStatement.setString(3, User.getLastName());
  15. preparedStatement.setString(4, User.getUsername());
  16. preparedStatement.setString(5, User.getPassword());
  17. preparedStatement.setString(6, User.getEmail());
  18. System.out.println(preparedStatement);
  19. // Step 3: Execute the query or update query
  20. result = preparedStatement.executeUpdate();
  21. } catch (SQLException e) {
  22. // process sql exception
  23. printSQLException(e);
  24. }
  25. return result;
  26. }

Thank you.

答案1

得分: 4

你总是通过preparedStatement.setInt(1, 1)id列的值设置为1。既然你说id列是自增的,请从你的SQL中移除id,删除上述行,并调整所有其他setString()方法调用的列索引(第一个参数)。

英文:

You are always setting the value of your id column to 1 via preparedStatement.setInt(1, 1). Since you say you have an auto-increment on the id column, remove id from your SQL, remove the aforementioned line and adjust the column indexes (first arguments) for all the other setString() method calls.

答案2

得分: 1

自动递增设置后,您无需在SQL插入语句中指定 'id' 列。

这是关于MySQL官方网站上自动递增的一些信息:https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html

英文:

With auto increment set you don’t have to specify the ‘id’ column in your sql insert statements.

Here’s some information on the auto increment from MySQL’s website: https://dev.mysql.com/doc/refman/8.0/en/example-auto-increment.html

答案3

得分: 0

从您的代码看,似乎您总是为id列插入值1,请正确实现autoGenerate。有关更多帮助,您可以查看类似的实现

英文:

From your code it looks like you are inserting the value 1 always for the id column, try implementing the autoGenerate properly. For more help, you can have a look on similar implementation

huangapple
  • 本文由 发表于 2020年8月2日 22:27:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63217143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定