2014-02-20 29 views
0

更新方法所以,我有一個更新的方法來更新的客戶,它看起來像:如何測試在JDBC

public static Customer UpdateCustomer(Customer customer){ 
    System.out.println("Updating customer "); 


    try { 
     statement = connection.createStatement(); 
     statement.executeUpdate("UPDATE customer " + "SET id = " + customer.getCustomerId() + "SET name = " + customer.getName() + "SET tagNo = " + customer.getTagNo() + 
       "SET telephoneNo = " + customer.getTelephoneNo() + "SET email = " + customer.getEmail() + "SET noOfTimesRecycles = " + 
       customer.getNoOfTimesRecycled()); 


    } catch (SQLException ex) { 
     System.out.println("Error in updating customer"); 
     ex.printStackTrace(); 
    } 

    return customer; 
} 

我不知道怎麼雖然測試。我所有的錢高達現在的測試位是這樣的:

public static Customer UpdateCustomer(Customer customer){ 

    return UpdateCustomer(customer); 
} 

public static void main(String [] args) 
{ 
    connectTest(); 

    UpdateCustomer(105,"John", 4179, "+4475855216"); 

      closeTest(); 
} 

那麼它給了我錯誤的UpdateCustpmer因爲它預計類型的客戶的東西。

能anyonw幫助我?謝謝

+0

你有堆棧跟蹤嗎? – sadhu

+0

是的,我做的是更新方法 – Sne

+0

只是一句話:你的更新語句不應該更新客戶ID,而是把它作爲一個地方標準,尋找客戶更新 – wxyz

回答

0

簡單的測試方法如下。

公共靜態客戶UpdateCustomer(客戶的客戶){ 的System.out.println( 「更新客戶」);

try { 
    statement = connection.createStatement(); 
    String query = "UPDATE customer " + "SET id = " + customer.getCustomerId() + "SET name = " + customer.getName() + "SET tagNo = " + customer.getTagNo() + 
      "SET telephoneNo = " + customer.getTelephoneNo() + "SET email = " + customer.getEmail() + "SET noOfTimesRecycles = " + 
      customer.getNoOfTimesRecycled(); 

    System.out.println("Update query is:::"+query); 
    statement.executeUpdate(query); 


} catch (SQLException ex) { 
    System.out.println("Error in updating customer"); 
    ex.printStackTrace(); 
} 

return customer; 

}

現在,複製的日誌「更新查詢」並粘貼到數據庫控制檯(MySQL或找你使用任何其它數據庫)。並在那裏檢查錯誤消息。

+0

如果你發現它按照你的期望,那麼你可以投票它按照stackoverflow標準。 –

2

你需要一個Customer對象傳遞給UpdateCustomer方法,創建一個Customer對象,並使用你的價值填充它,然後將它傳遞給UpdateCustomer方法,如:

public static void main(String [] args) 
{ 
    connectTest(); 
    Customer customer = new Customer(105,"John", 4179, "+4475855216");// note you should have a constructor takes the passed arguments. or you could use setter methods 
    UpdateCustomer(customer); 

    closeTest(); 
} 

你需要閱讀關於methods和Java中constructors

編輯:

你並不需要使用SET關鍵字每個T ime,只是分開你想更新昏迷的列,如:

public static Customer UpdateCustomer(Customer customer){ 
    System.out.println("Updating customer "); 


    try { 
     statement = connection.createStatement(); 
     statement.executeUpdate("UPDATE customer " + "SET id = " + customer.getCustomerId() + ",name = " + customer.getName() + ",tagNo = " + customer.getTagNo() + 
      ",telephoneNo = " + customer.getTelephoneNo() + ",email = " + customer.getEmail() + "SET noOfTimesRecycles = " + 
      customer.getNoOfTimesRecycled()); 


    } catch (SQLException ex) { 
     System.out.println("Error in updating customer"); 
     ex.printStackTrace(); 
    } 

    return customer; 
} 
+0

我有客戶類中的構造函數。這個主要方法在我的測試類中。在該類中創建客戶對象是否正常? – Sne

+0

是的,你應該如何測試'Customer'類而不從它創建對象。 – Salah

+0

仍然收到錯誤。 我的代碼: >客戶客戶=新的客戶(106, 「佩頓萊伊」,4517, 「44745271478」, 「[email protected]」,1); \t \t UpdateCustomer(customer); \t \t 它說:在com.qmul.rfid.dataaccess.DatabaseInterfaceTest.UpdateCustomer(DatabaseInterfaceTest錯誤。java:46) – Sne