[英]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.