java.sql.SQLIntegrityConstraintViolationException

发布于 2024-10-16 02:51:06 字数 3800 浏览 0 评论 0原文

我在运行保存插入/更新命令时收到此错误:

SEVERE: null
java.sql.SQLIntegrityConstraintViolationException: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL110207185137350' defined on 'EMPLOYEE'.
null, Boris Wilkins

如果有人能找出发生此错误的原因,那就太好了。

这是代码:

 /////////////////////////////////////////////
    ///   UPDATE methods
    /** Saves an existing pkg in the database */
    public void save(Employee pkg) throws DataException {
        Connection conn = null;
        try {
            conn = ConnectionPool.getInstance().get();
            save(pkg, conn);
            conn.commit();

        } catch (Exception e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                throw new DataException("We're currently upgrading our site to serve you better.", e);
            }
            throw new DataException("Problem saving the Employee", e);

        } finally {
            ConnectionPool.getInstance().release(conn);

        }//update
    }

    /** Internal method to update a pkg in the database */
    void save(Employee emp, Connection conn) {
        // update the cache
        Cache.getInstance().put(emp.getId(), emp);
        // if not dirty, return
        if (!emp.isDirty()) {
            return;
        }
        // call either update() or insert()
        if (emp.isObjectAlreadyInDB()) {
            update(emp, conn);
        } else {
            insert(emp, conn);
        }

    }//save

    /** Saves an existing pkg to the database */



    //NEED HELP WITH EXECUTE STATEMENT!!!


    private void update(Employee pkg, Connection conn) {
        try {
            // run the update SQL
            PreparedStatement pstmt = conn.prepareStatement("UPDATE Employee SET id=?, name1=?, username1=?, password=?, type=? WHERE id=?");
            pstmt.setString(1, pkg.getId());
            pstmt.setString(2, pkg.getName1());
            pstmt.setString(3, pkg.getUserName1());
            pstmt.setString(4, pkg.getPassword());
            pstmt.setString(5, pkg.getType());
            pstmt.setString(6, pkg.getId());

            pstmt.executeUpdate();
            pstmt.close();
            // update the dirty variable
            pkg.setDirty(false);
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeDAO.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    /** Inserts a new pkg into the database */
    private void insert(Employee pkg, Connection conn) {
        try {
            // run the insert SQL
            PreparedStatement pstmt = conn.prepareStatement("INSERT into Employee (id, name1, username1, password, type) values(?, ?, ?, ?, ?)");
            pstmt.setString(1, pkg.getId());
            pstmt.setString(2, pkg.getName1());
            pstmt.setString(3, pkg.getUserName1());
            pstmt.setString(4, pkg.getPassword());
            pstmt.setString(5, pkg.getType());
            // update the dirty variable

            pstmt.execute();
            pstmt.close();
            pkg.setDirty(false);
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

alreadyInDB 的代码:

/** Returns whether the object is in the DB or not */
  boolean isObjectAlreadyInDB() {
    return objectAlreadyInDB;
  }//isObjectAlreadyInDB

  /** 
   * Sets whether the object is already in the DB.  This method
   * is called ONLY from the DAO responsible for this object.
   */
  void setObjectAlreadyInDB(boolean objectAlreadyInDB) {
    this.objectAlreadyInDB = objectAlreadyInDB;
  }//setObjectAlreadyInDB

I am getting this error when running a save insert/update command:

SEVERE: null
java.sql.SQLIntegrityConstraintViolationException: The statement was aborted because it would have caused a duplicate key value in a unique or primary key constraint or unique index identified by 'SQL110207185137350' defined on 'EMPLOYEE'.
null, Boris Wilkins

If anyone can spot why this error is happening it would be great.

here is the code:

 /////////////////////////////////////////////
    ///   UPDATE methods
    /** Saves an existing pkg in the database */
    public void save(Employee pkg) throws DataException {
        Connection conn = null;
        try {
            conn = ConnectionPool.getInstance().get();
            save(pkg, conn);
            conn.commit();

        } catch (Exception e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                throw new DataException("We're currently upgrading our site to serve you better.", e);
            }
            throw new DataException("Problem saving the Employee", e);

        } finally {
            ConnectionPool.getInstance().release(conn);

        }//update
    }

    /** Internal method to update a pkg in the database */
    void save(Employee emp, Connection conn) {
        // update the cache
        Cache.getInstance().put(emp.getId(), emp);
        // if not dirty, return
        if (!emp.isDirty()) {
            return;
        }
        // call either update() or insert()
        if (emp.isObjectAlreadyInDB()) {
            update(emp, conn);
        } else {
            insert(emp, conn);
        }

    }//save

    /** Saves an existing pkg to the database */



    //NEED HELP WITH EXECUTE STATEMENT!!!


    private void update(Employee pkg, Connection conn) {
        try {
            // run the update SQL
            PreparedStatement pstmt = conn.prepareStatement("UPDATE Employee SET id=?, name1=?, username1=?, password=?, type=? WHERE id=?");
            pstmt.setString(1, pkg.getId());
            pstmt.setString(2, pkg.getName1());
            pstmt.setString(3, pkg.getUserName1());
            pstmt.setString(4, pkg.getPassword());
            pstmt.setString(5, pkg.getType());
            pstmt.setString(6, pkg.getId());

            pstmt.executeUpdate();
            pstmt.close();
            // update the dirty variable
            pkg.setDirty(false);
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeDAO.class.getName()).log(Level.SEVERE, null, ex);
        }


    }

    /** Inserts a new pkg into the database */
    private void insert(Employee pkg, Connection conn) {
        try {
            // run the insert SQL
            PreparedStatement pstmt = conn.prepareStatement("INSERT into Employee (id, name1, username1, password, type) values(?, ?, ?, ?, ?)");
            pstmt.setString(1, pkg.getId());
            pstmt.setString(2, pkg.getName1());
            pstmt.setString(3, pkg.getUserName1());
            pstmt.setString(4, pkg.getPassword());
            pstmt.setString(5, pkg.getType());
            // update the dirty variable

            pstmt.execute();
            pstmt.close();
            pkg.setDirty(false);
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

Code for alreadyInDB:

/** Returns whether the object is in the DB or not */
  boolean isObjectAlreadyInDB() {
    return objectAlreadyInDB;
  }//isObjectAlreadyInDB

  /** 
   * Sets whether the object is already in the DB.  This method
   * is called ONLY from the DAO responsible for this object.
   */
  void setObjectAlreadyInDB(boolean objectAlreadyInDB) {
    this.objectAlreadyInDB = objectAlreadyInDB;
  }//setObjectAlreadyInDB

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红玫瑰 2024-10-23 02:51:06

这条语句 emp.isObjectAlreadyInDB() 显然失败了,并且始终返回 false,导致每次运行代码时都会插入相同的员工,从而违反了主键或您在该数据库表中设置的唯一约束。

This statement emp.isObjectAlreadyInDB() is obviously failing and returning false all the time, causing the same employee to be inserted everytime you run the code, thus violating the primary key or unique constraint you set up in that database table.

萌辣 2024-10-23 02:51:06

我正在使用测试器类,并且意外地创建了一个与数据库中已有对象具有相同 id 的对象。

I am using a tester class and i was accidentally creating an object with the same id as an object that was already in the DB.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文