有人能指出我的代码有什么问题吗?我有 2 个表,我想从 2 个表中删除一行。我能够从 1 个表中删除一行,但从 2 个表中删除一行不起作用。这是我的代码:
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();
}
我得到的错误
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
非常感谢!
最佳答案
你的约束应该更像是......
WHERE driverID = '1' and carID = '1'
我还强烈建议您使用 PreparedStatement
参见 Using PreparedStatement's了解更多详情
关于java - 从Java中的2个表中删除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29688144/