I'm not sure how to fix this. I tested the SQL statement in MySQLWorkbench and it works.
public void updateTable() throws SQLException
{
String query = "SET SQL_SAFE_UPDATES = 0; drop table if exists studentCopy; create table studentCopy select * from student;\n" +
"update studentCopy join (select ID, sum(credits) new_tot_cred\n" +
"from takes left join course using(course_id) where grade is not null and grade<>'F'\n" +
"group by ID) cred using(ID) set tot_cred=cred.new_tot_cred;\n" +
"SET SQL_SAFE_UPDATES = 1;\n" +
"select * from studentCopy;";
resultSet = statement.executeQuery(query);
}
java.sql.SQLSyntaxErrorException: 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 'drop table if exists studentCopy; create table studentCopy select * from student' at line 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.StatementImpl.executeQuery(StatementImpl.java:1198)
at MyQuery.updateTable(MyQuery.java:157) // resultSet = statement.executeQuery(query);
at TestMyQuery.main(TestMyQuery.java:38) // main method call
This query worked in workbench.
SET SQL_SAFE_UPDATES = 0;
drop table studentCopy;
create table studentCopy select * from student;
update studentCopy join (select ID, sum(credits) new_tot_cred
from takes left join course using(course_id) where grade is not null and grade<>'F'
group by ID) cred using(ID) set tot_cred=cred.new_tot_cred;
SET SQL_SAFE_UPDATES = 1;
select * from studentCopy;
SQL_SAFE_UPDATESdoes? Why copy the table? Aren't you just after a SELECT form of your UPDATE query?