简体   繁体   English

如何使用Java代码比较VARCHAR数据类型以确保数据库中的数据相同

[英]How to compare VARCHAR data types using Java code to make sure the data is the same in the database

How do I compare the value in two different databases with the same data in the columns because of data migration. 由于数据迁移,如何将两个不同数据库中的值与列中的相同数据进行比较。 I am running jUnit Tests and the values are not passing the test as the same. 我正在运行jUnit测试,并且值未通过相同的测试。 What am I doing wrong? 我究竟做错了什么? Right know I have the sourceDB and the target database set to the same database, ie they are they same thing. 知道我将sourceDB和目标数据库设置为同一数据库,即它们是同一件事。 I did this because I want to be sure the connection was correct throughout the process and narrow down any problems to syntax errors. 我这样做是因为我想确保连接在整个过程中都是正确的,并将所有问题缩小到语法错误。 The value in the rows of the name column is; 名称列各行中的值为: Initiative Management, Bugs & Enhancements, Hardware & Software Requests. 计划管理,错误和增强功能,硬件和软件请求。

public class DemandTypeCopier {

    public static String QUERY_CREATECOPY = "select name from tr_demandtype where name=?";

}

public void testQUERY_CREATECOPY() throws Exception{

    UnitTestHelper helper = new UnitTestHelper();
    Connection con = helper.getConnection(helper.sourceDBUrl);
    Connection conTarget = helper.getConnection(helper.targetDBUrl);
    String x = "Initiative management";

    PreparedStatement stmt = con.prepareStatement(DemandTypeCopier.QUERY_CREATECOPY);
    stmt.setString(1, x);
    ResultSet sourceVal = stmt.executeQuery();

    stmt = conTarget.prepareStatement(DemandTypeCopier.QUERY_CREATECOPY);
    stmt.setString(1, x);
    ResultSet targetVal = stmt.executeQuery();

    assertTrue(helper.resultSetsEqual(sourceVal,targetVal));

}

public boolean resultSetsEqual (ResultSet source, ResultSet target) throws SQLException{
    while(source.next())
    {
            target.next();
            ResultSetMetaData metadata = source.getMetaData();
            int count = metadata.getColumnCount();
            for (int i =1; i<=count; i++)
            {
               if(source.getObject(i) != target.getObject(i))
               {
              return false;
               }
            }
    }
    return true;
}

The problem is that when comparing the data, you are testing object identity instead of equality. 问题在于,在比较数据时,您正在测试对象身份而不是相等性。

source.getObject(i) != target.getObject(i)

The above will test if the objects returned from both resultsets are the same which will not be true in most (all?) cases. 上面的代码将测试从两个结果集中返回的对象是否相同 ,这在大多数(全部?)情况下都是不正确的。 What you really want to do is test for equality: 您真正想做的是测试是否相等:

source.getObject(i).equals(target.getObject(i))

Note that the above would throw a NullPointerException if source.getObject(i) returns null, so unless null values are not allowed for any of the columns in the database, you would need to add code to handle null values. 请注意,如果source.getObject(i)返回null,则以上内容将引发NullPointerException,因此,除非数据库中的任何列均不允许使用null值,否则需要添加代码以处理null值。

Add an ORDER BY clause to your query, otherwise the order of the returned results is not guaranteed. 在查询中添加ORDER BY子句,否则不能保证返回结果的顺序。

Check the result of target.next() so that you correctly handle the case when the size of the two resultsets are not equal. 检查target.next()的结果,以便当两个结果集的大小不相等时正确处理这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何比较两个MimeMessage.java对象以确保它们是相同的? - How to compare two MimeMessage.java objects to make sure they are the same? 如何使用Java SQL将数据类型插入到数据库表中 - how to insert into database table with different data types using Java SQL 如何使用Hibernate从数据库中检索列的Java数据类型? - How to retrieve the java data types of columns from the database using Hibernate? 如何在Java中比较两个相同对象的数据 - How to compare the data of two same Objects in Java 我应该如何确定数据库中是否已经存在数据 - How should i make sure if the data already exists in the database 如何在Java中记录多种数据类型? - How to make a record of multiple data types in java? 如何在Java中解析/广播(不确定术语)未知数据类型 - How do you parse/cast (not sure of the term) unknown data types in java 如何比较对象的私有数据字段以确保它们是相同的(Java)? - How to compare private data fields of objects to ensure they are the same (Java)? 如何在java中比较2种不同的数据库列数据类型 - how to compare 2 different database column data type in java 如何解决此错误:数据类型text和varchar在等于运算符中不兼容? - How to resolve this error: The data types text and varchar are incompatible in the equal to operator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM