Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在jdbc中测试更新方法_Java_Database_Testing_Sql Update - Fatal编程技术网

Java 如何在jdbc中测试更新方法

Java 如何在jdbc中测试更新方法,java,database,testing,sql-update,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

因此,我有一个更新方法来更新客户,它看起来像:

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();
}
它在UpdateCustomer上给了我一个错误,因为它需要customer类型的东西


有人能帮我吗?谢谢

您需要将
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中的和

编辑:

您不需要每次都使用
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;
}

测试的简单方法如下所示

公共静态客户更新用户(客户){ System.out.println(“更新客户”)

}


现在,复制“updatequeryis”日志并粘贴到数据库控制台(mysql或您正在使用的任何其他数据库)。并检查其中的错误消息。

您是否有堆栈跟踪?是的,我在该更新方法中有。请注意:您的update语句不应更新customerId,而应将其用作查找要更新的客户的where标准。它是这样说的:线程“main”中的异常java.lang.Error:未解决的编译问题:DatabaseInterfaceTest类型中的方法UpdateCustomer(Customer)不适用于com.qmul.rfid.dataaccess.DatabaseInterfaceTest.main(DatabaseInterfaceTest.java:97)中的参数(int,String,int,String)@wxyz我想你说的是我真正想做的,但我不确定如何测试该方法是否有效。有什么建议吗?我在customer类中有构造函数。这个主要方法在我的测试课上。在那个类中创建一个客户对象可以吗?是的,你应该,你如何测试
customer
类而不从中创建一个对象。仍然会出错。我的代码:>客户=新客户(106,“Payton Ley”,4517,“+44745271478”,“p。ley@gmail.com", 1); UpdateCustomer(客户);它说:com.qmul.rfid.dataaccess.DatabaseInterfaceTest.UpdateCustomer(DatabaseInterfaceTest.java:46)上的错误这是您上面提到的错误之外的另一个错误,这个错误是由
UpdateCustomer
方法引起的。查看我的更新答案。尝试过,但仍然得到相同的错误!无论如何谢谢你!如果您发现它符合您的期望,那么您可以按照stackoverflow标准进行投票。
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;