Search code examples
javamysqljdbcdelete-row

deleting tables on MySQL from jdbc


Problem when deleting tables from jdbc

I have a. jar that deletes records from multiple tables through a previously prepared statement

i did it this way:

-- SQL
delete from tabla_a using tabla_a join tabla_c join tabla_b join tabla_d
where tabla_a.tabla_c_id = tabla_c.id and tabla_c.tabla_b_id = tabla_b.id
and tabla_b.tabla_d_id = tabla_d.id and tabla_d.field = 2;

note: where (?) is an integer

// Java
conn = DriverManager.getConnection(dsn, user, pass);
stmt = conn.createStatement();
int rows = stmt.executeUpdate(query);

does not work does not erase any record, I tested the SQL directly to MySQL and working properly.


Solution

  • Try using a PreparedStatement instead, and set the parameters. Something along these lines:

    conn = DriverManager.getConnection(dsn, user, pass);
    stmt = conn.prepareStatement(query);
    stmt.setInt(1, 2);
    int rows = stmt.executeUpdate();