java.sql.SQLIntegrityConstraintViolationException异常

标签 java sql derby

运行保存插入/更新命令时出现此错误:

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

最佳答案

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

关于java.sql.SQLIntegrityConstraintViolationException异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4928943/

相关文章:

java - 在这种情况下,是创建通用 DAO 还是将其与较小的 DAO 分开更好?

java - 如何使用 DES 在 Java 中加密数据并将该数据解密为 PHP

java - 使用 JSF 从用户获取日期输入并存储在数据库中时出错

java - 为什么 Derby 数据库占用这么多空间?

java - 如何在Hibernate注释中指定char列的大小?

在 JVM 中记录/重放执行代码的 Java 解决方案

sql - 选择年份 = 特定的年份范围

sql - 查询检索与一名学生同一类(class)的所有学生

mysql - 从表中选择,其中值在同一表的另一列上重复

java - 嵌入式 Derby/Java DB 中的错误自动增量