我正在尝试编写一个程序,删除所有数据从数据库(MariaDB)使用JDBC,但我得到这个错误:
Exception in thread "main" java.lang.RuntimeException: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 1
at com.iffi.AccountData.clearDatabase(AccountData.java:35)
at com.iffi.AccountData.main(AccountData.java:679)
Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' 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.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1098)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1046)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1371)
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1031)
at com.iffi.AccountData.clearDatabase(AccountData.java:32)
... 1 more
下面是我的代码:
public class AccountData {
/**
* Removes all records from all tables in the database.
*/
public static void clearDatabase() {
Connection conn = ConnectionPool.getConnection();
PreparedStatement ps = null;
List<String> tables = Arrays.asList("table1", "table2", "table3", "table4", "table5", "table6", "table7", "table8");
for (String table: tables) {
String query = "Delete from" + table + ";";
try {
ps = conn.prepareStatement(query);
ps.executeUpdate();
} catch (SQLException e) {
ConnectionPool.LOG.error("SQL Exception: ", e);
throw new RuntimeException(e);
}
}
try {
ps.close();
ConnectionPool.putConnection(conn);
} catch (SQLException e) {
ConnectionPool.LOG.error("SQL Exception: ", e);
throw new RuntimeException(e);
}
}
}
你知道我可能做错了什么吗?
尝试在查询字符串
中添加一个空格