2017-02-21 72 views
0

我有一個Java項目爲學校。我使用sql和JFrame工作,當我嘗試從訂單中刪除(或編輯)記錄時,訂單的所有記錄都被刪除而不是一個,所以我有3個表格(客戶,訂單,汽車)。我猜我的SQL查詢有問題。 因此,如果任何人都可以告訴我我的錯誤,那就太好了。 :)爲什麼我的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 
+1

我希望它看起來像這樣:'SQL = 「從訂單刪除其中cus_id =」 + selectCustomerId +「AND car_id =」+ selectCarId;' –

回答

1

你只是增加了兩個數字,我真的不知道爲什麼。我認爲不是:

String sql="delete from Orders where " + selectCustomerId + selectCarId;

你應該寫:

String sql="delete from Orders where cus_id =" + selectCustomerId +" and car_id = "+ selectCarId;

相關問題