2011-02-08 7 views
1

私はこのエラーを取得しています:java.sql.SQLIntegrityConstraintViolationException

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 

このエラーは、それは素晴らしいことだ何が起こっている理由を誰でも見つけることができます。

ここのコードです:alreadyInDBため

///////////////////////////////////////////// 
    /// 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); 
     } 
    } 

コード:

/** 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 
+0

更新ステートメントがIDを設定するのはなぜですか?他のフィールドだけを設定するべきではありませんか?それが問題だとは思わないが、奇妙に思える。 – rfeak

+1

実際にもう少し考えてみると、問題の原因ではないことをもう一度確認したいかもしれません。 DBは私の強みではありませんが、既に存在するプライマリキーを更新しようとしていると不平を言うDBを見ることができました。少なくともそれを可能性として排除する。 – rfeak

+0

ご覧いただきありがとうございます。私はそれを変更しましたが、私は間違いなく同じエラーが発生しました。 – novicePrgrmr

答えて

3

この文emp.isObjectAlreadyInDB()は明らかに同じ従業員は、実行毎回挿入されることを引き起こして、すべての時間を失敗し、falseを返しているが、そのため、データベーステーブルに設定したプライマリキーまたはユニーク制約に違反します。

2

なぜ私のコードが悪いのかが分かりました。

私はテスタークラスを使用しています。私は誤って既にDBに入っていたオブジェクトと同じIDを持つオブジェクトを作成していました。

助けてくれてありがとう!

関連する問題