Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.sql.SQLIntegrityConstraintViolationException_Java_Sql_Derby - Fatal编程技术网

java.sql.SQLIntegrityConstraintViolationException

java.sql.SQLIntegrityConstraintViolationException,java,sql,derby,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'

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

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
,导致每次运行代码时都插入同一名员工,因此违反了您在该数据库表中设置的主键或唯一约束。

我使用的是tester类,我无意中创建了一个与数据库中已有的对象具有相同id的对象。

为什么update语句设置了id?您不应该只设置其他字段吗?不确定这是否是一个问题,但似乎很奇怪。实际上,再仔细考虑一下,您可能需要再次检查这不是问题的根源。DBs不是我的强项,但我可以看到DB抱怨试图更新已经存在的主键。至少要排除这种可能性。谢谢你看到了。我改变了它,我确实需要这样做,但仍然得到相同的错误。