java - 如何测试 jdbc 中的更新方法

标签 java database testing sql-update

所以我有一个更新客户的更新方法,它看起来像:

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/

相关文章:

java - 使用 hashmap java 返回字典

java - JMock assertIsSatisfied 在 TearDown 中?

mysql - 当总计在 mysql 中并列时,将优先级设置为一行

java - 需要帮助设计员工和经理关系

ruby-on-rails - 如何使用 WATIR 检测 jquery UI 对话框元素?

c# - 是否有 Visual Studio Load Test 开源等效项 - 或者类似的东西?

python - 如何在机器人框架中获取错误信息?

java - Gradle 子项目无法识别根 build.gradle 中的第 3 方依赖项

java - 源和目标的 Maven 编译器插件 jdk 版本

mysql - 如何确定查询已处理的数据库表行?