Need to implement an update to two linked tables in that, I need to get the last value from first table's auto-increment key to be used in subsequent insert to the linked table.
Within MySQL, can get the value using LAST_INSERT_ID() function but tying two of the insert statements with ebase might be failing at the second tables insert.
What I have used.
1). tried to create the string for both insert statements as would work in Mysql into a sql variable to use in generating the stmt as below
Code: Select all
stmt = conn.prepareStatement(sql1);
2). Split the two SQL statements, insert first as in statements below
sql1 = "INSERT INTO client \
(`firstname`, `middlename`, `lastname`)\
\nVALUES('abc123','','abc123');"
sql2 = "INSERT INTO credentials \
(`username`, `password`, `Client_student_id`)\
\nVALUES ('abc123', 'abc123', LAST_INSERT_ID());"
Code: Select all
stmt = conn.prepareStatement(sql1);
stmt.addBatch(sql2)
//execute
stmt.executeBatch();
Any other way to capture last value in initial insert to be used in formulating the second SQL statement?
UPDATE 1
Please clarify for me the operation for creating a preparestatement since appears my code is ignoring what I assumed was added as the first statement via the line
stmt = conn.prepareStatement(sql1);
but if I add my statements again, the execution works fine.
Would still be ideal to know other ebase way of capturing the auto-generated keys.
Regards[/u]