Duplicate entry for primary key – registration form

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

Duplicate entry for primary key - registration form

问题

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

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

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

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

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

public class UserDAO {

    public int registerUser(User User) throws ClassNotFoundException, SQLException {
        String INSERT_USERS_SQL = "INSERT INTO user"
                + "  (id, firstName, lastName, username, password, email) VALUES "
                + " (?, ?, ?, ?, ?, ?)";

        int result = 0;

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

        try (Connection connection = DriverManager
                .getConnection("jdbc:mysql://localhost:3306/cs230projekat","root","");
                // Step 2:Create a statement using connection object
                PreparedStatement preparedStatement = connection.prepareStatement(INSERT_USERS_SQL)) {
            preparedStatement.setInt(1, 1);
            preparedStatement.setString(2, User.getFirstName());
            preparedStatement.setString(3, User.getLastName());
            preparedStatement.setString(4, User.getUsername());
            preparedStatement.setString(5, User.getPassword());
            preparedStatement.setString(6, User.getEmail());

            System.out.println(preparedStatement);
            // Step 3: Execute the query or update query
            result = preparedStatement.executeUpdate();

        } catch (SQLException e) {
            // process sql exception
            printSQLException(e);
        }
        return result;
    }
}

谢谢。

英文:

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:

public class UserDAO {

public int registerUser(User User) throws ClassNotFoundException, SQLException {
    String INSERT_USERS_SQL = "INSERT INTO user"
            + "  (id, firstName, lastName, username, password, email) VALUES "
            + " (?, ?, ?, ?, ?, ?)";

    int result = 0;

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

    try (Connection connection = DriverManager
            .getConnection("jdbc:mysql://localhost:3306/cs230projekat","root","");
            // Step 2:Create a statement using connection object
            PreparedStatement preparedStatement = connection.prepareStatement(INSERT_USERS_SQL)) {
        preparedStatement.setInt(1, 1);
        preparedStatement.setString(2, User.getFirstName());
        preparedStatement.setString(3, User.getLastName());
        preparedStatement.setString(4, User.getUsername());
        preparedStatement.setString(5, User.getPassword());
        preparedStatement.setString(6, User.getEmail());

        System.out.println(preparedStatement);
        // Step 3: Execute the query or update query
        result = preparedStatement.executeUpdate();

    } catch (SQLException e) {
        // process sql exception
        printSQLException(e);
    }
    return result;
}

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:

确定