How to insert into two different tables in one statement with Java and MySQL?
Posted
by MalcomTucker
on Stack Overflow
See other posts from Stack Overflow
or by MalcomTucker
Published on 2010-03-12T00:10:39Z
Indexed on
2010/03/12
0:47 UTC
Read the original article
Hit count: 271
I am using Java, Spring (NamedParameterJdbcTemplate) and MySQL. My statement looks like this:
INSERT INTO Table1 (Name) VALUES (?);
INSERT INTO Table2 (Path, Table1Id) VALUES (?, LAST_INSERT_ID())
But it is throwing the following error:
PreparedStatementCallback; bad SQL grammar [INSERT INTO Table1 (Name) VALUES (?);
INSERT INTO Table2 (Path, Table1Id) VALUES (?, LAST_INSERT_ID())
] `
Nested exception is:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INSERT INTO Table2 (Path, Table1Id' at line 1
The syntax works fine in MySQL but something is up when combining via the Spring template.
Thanks!
© Stack Overflow or respective owner