所以我有一个更新客户的更新方法,它看起来像:
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 处给了我错误,因为它期待某种类型的客户。
有人可以帮我解决这个问题吗?谢谢
最佳答案
您需要将一个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 的内容和 constructors在Java中
编辑:
你不需要每次都使用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;
}
关于java - 如何测试 jdbc 中的更新方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21904400/