我有一个Java项目可供学校使用。我使用sql和JFrame并且当我尝试从订单中删除(或编辑)记录时,我有3个表(客户,订单,汽车),所有来自订单的记录都被删除而不是一个。我猜我的SQL查询有问题。 所以,如果有人能告诉我我的错误那就太好了。 :)
这是我的代码:
class DeleteOrder implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
conn = DBConnector.getConnected();
String selectCustomerId = "select cus_id from customers where lname=? and ";
String selectCarId = "select car_id from cars where model=?";
String selectedCustomer = customersCombo.getSelectedItem().toString();
String selectedCar = carsCombo.getSelectedItem().toString();
String sql="delete from Orders where " + selectCustomerId + selectCarId;
try {
state = conn.prepareStatement(sql);
state.setString(1, selectedCustomer);
state.setString(2, selectedCar);
state.execute();
getAllDataFromOrders();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}//end Delete ORder
答案 0 :(得分:1)
你只是添加两个数字,我真的不知道为什么。我想而不是:
String sql="delete from Orders where " + selectCustomerId + selectCarId;
你应该写:
String sql="delete from Orders where cus_id =" + selectCustomerId +" and car_id = "+ selectCarId;