Does anyone know what happened to my code? I use the simple INSERT statement (unlike the regular PreparedStatement, because I am trying to use the mySQL functions AES_ENCRYPT / DECRYPT. I don’t know how to use them with the usual PreparedStatements setting with all the funky question marks.
I keep getting classics:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Unable to add or update a child row: foreign key constraint ends with an error ( video_game_db. login, CONSTRAINT login_ibfk_1FOREIGN KEY ( cust_ID) LINKS customer( cust_ID))
Of course, it makes me chat.
I spent the whole night trying to do the usual JDBC implementation of the encrypted “stuff”. In the end, I could only encrypt strings, but could not decrypt them. I decided that he needed to do something with mySQL, not JDBC, because before I entered them into the database, they decrypted everything, but then I was able to get it to work.
Please take a look at my DDL and DML JDBC table instructions 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
{
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);