Marko360
Marko360

Reputation: 13

Getting error in SQL while trying to delete records with JDBC

I am trying to write a program that deletes all data from the database (MariaDB) using JDBC but I'm getting this error:

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

Here's my code:

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);
        }
    }
}

Any ideas on what I might be doing wrong?

Upvotes: 0

Views: 806

Answers (1)

Andrew Stowe
Andrew Stowe

Reputation: 11

Try adding a space after from in your query string

Upvotes: 1

Related Questions