所以我有一个更新客户的更新方法,它看起来像:
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上给我错误,因为它期待Type客户的某些东西。
任何人都可以帮助我吗?谢谢
答案 0 :(得分: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();
}
您需要阅读java中的methods和constructors
编辑:
您每次都不需要使用SET
关键字,只需将要更新的列分隔为昏迷,例如:
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;
}
答案 1 :(得分:0)
简单的测试方法如下。
public static Customer UpdateCustomer(Customer customer){ 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;
}
现在,复制“update query is”的日志并粘贴到数据库控制台(mysql或任何其他使用的数据库)中。并检查错误信息。