title: Cannot add or update a child row: a foreign key constraint fails
body: <p>Anybody know what's wrong with my code?  I am using a simple INSERT statement (as opposed to the regular PreparedStatement because I am trying to utilize mySQL's AES_ENCRYPT/DECRYPT functions.  I do not know how to use them with the regular set up of the PreparedStatements. with all the funky question marks.  </p>
  
  <p>I keep getting the classic:</p>
  
  <blockquote>
    <p>com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (<code>video_game_db</code>.<code>login</code>, CONSTRAINT <code>login_ibfk_1</code> FOREIGN KEY (<code>cust_ID</code>) REFERENCES <code>customer</code> (<code>cust_ID</code>))</p>
  </blockquote>
  
  <p>Of course, it's driving me batty. </p>
  
  <p>I spent all night trying to make the regular JDBC implementation of the cipher "stuff" work.  In the end, I could only encrypt the strings, but could not decrypt them.  I figured it had to do something with mySQL as opposed to JDBC, as before I input them into the database they decrypted fine, but afterwards I could get it to work. </p>
  
  <hr>
  
  <p>Please take a look at my JDBC table DDL and DML statements below:</p>
  
  <p><strong>DDL</strong></p>
  
  <pre><code>Statement s = conn.createStatement();
  s.executeUpdate("CREATE TABLE customer("+"cust_ID CHAR(10) NOT NULL,"+"PRIMARY KEY(cust_ID),"+"first_Name CHAR(30)NOT NULL,mI CHAR(2),last_Name CHAR(50)NOT NULL,street_Name CHAR(50),city CHAR(30) NOT NULL,state CHAR(50) NOT NULL,"
              +"zip_Code CHAR(5) NOT NULL, home_Phone CHAR(12) UNIQUE, referrer CHAR(30), quantity INTEGER NOT NULL, item_No CHAR(10))"); 
  s.executeUpdate("CREATE TABLE login ("+"user_Name CHAR(50) NOT NULL,"+"PRIMARY KEY(user_Name),"+"pass_Word CHAR(50)NOT NULL, cust_ID CHAR(10))");  
  </code></pre>
  
  <p><strong>DML</strong></p>
  
  <pre><code>public static void setCustTable(String cust_ID, String lName, String fName, String mI, String street_Name, String city, String state,
    String zip_Code, String home_Phone, String referrer, int quantity, String itemNo)throws IOException, SQLException 
  {
  
  // connect to database
  try
  {    
  
      PreparedStatement stat = conn.prepareStatement("INSERT INTO customer (cust_ID, first_Name, mI, last_Name, street_Name, city, state, zip_Code, home_Phone, referrer, quantity, item_No)"
                  + " VALUES (?,?,?,?,?,?,?,?,?,?,?,?)");
  
      stat.setString(1, cust_ID);
      stat.setString(2, fName);
      stat.setString(3, mI);
      stat.setString(4, lName);
      stat.setString(5, street_Name);
      stat.setString(6, city);
      stat.setString(7, state);
      stat.setString(8, zip_Code);
      stat.setString(9, home_Phone);
      stat.setString(10, referrer);
      stat.setInt(11, quantity);
      stat.setString(12, itemNo);
  
      stat.executeUpdate();
  }  
  
   catch (SQLException e) 
  {
      e.printStackTrace();
      throw e;
  }
  
  **********************************************************************************
  
  
  
  public static void insertLoginData(String username5, String password5, String custID5)throws IOException, SQLException, NoSuchAlgorithmException, InvalidKeyException
  
  {
      Statement s = conn.createStatement();
      String insert="INSERT INTO login VALUES('username5', AES_ENCRYPT('text','password5'),'custID5')";
      s.executeUpdate(insert);
  </code></pre>