1

Can anybody point what is wrong with my code? I have 2 tables and I am trying to delete a row from 2 tables. I was able to delete a row from 1 table but delete a row from 2 tables did not work. Here is my code:

String id = theId.getText();
String id2 = theId2.getText();
textArea.setText("");
try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/dealer", "root", "admin")) {
        stmt = connection.prepareStatement("DELETE FROM person, cars WHERE driverID = ?, carID = ?");
    if (stmt != null) {
        stmt.setString(1, id);
        stmt.setString(2, id2);
        stmt.executeUpdate();
    }

The error I got

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 'WHERE driverID
= '1', carID = '1'' at line 1

Thank you so much!

Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
John
  • 53
  • 8

2 Answers2

2

Try this

String id = theId.getText();
String id2 = theId2.getText();
textArea.setText("");
try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/dealer", "root", "admin")) {
        stmt = connection.prepareStatement("DELETE person, cars FROM person, cars WHERE driverID = ? AND carID = ?");
        if (stmt != null) 
        {
            stmt.setString(1, id);
            stmt.setString(2, id2);
            stmt.executeUpdate();
        }
Saagar Elias Jacky
  • 2,684
  • 2
  • 14
  • 28
2

Your constraints should be more like...

WHERE driverID = '1' and  carID = '1'

I'd also highly recommend that you make use of a PreparedStatement

See Using PreparedStatement'sfor more details

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366