简体   繁体   中英

Cannot add or update a child row: a foreign key constraint fails

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.

I keep getting the classic:

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails ( video_game_db . login , CONSTRAINT login_ibfk_1 FOREIGN KEY ( cust_ID ) REFERENCES customer ( cust_ID ))

Of course, it's driving me batty.

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.


Please take a look at my JDBC table DDL and DML statements below:

DDL

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))");  

DML

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

For what it worth, I don't see any insert into customer . Error message tells that there is a foreign key constraint from login to customer. So customer entry must exist prior to login insert.


Was there intention to quote 'username5'? I think it is a parameter to method but insert uses literal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM