有人能指出我的代码有什么问题吗?我有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
非常感谢你!
答案 0 :(得分: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 person, cars FROM person, cars WHERE driverID = ? AND carID = ?");
if (stmt != null)
{
stmt.setString(1, id);
stmt.setString(2, id2);
stmt.executeUpdate();
}
答案 1 :(得分:2)
你的约束应该更像......
WHERE driverID = '1' and carID = '1'
我还强烈建议您使用PreparedStatement
有关详细信息,请参阅Using PreparedStatement's