当我将此 QUERY 发送到数据库时,我不断收到断言错误,即使当我手动将查询输入到两个数据库中时,它也会返回正确的数据。你能告诉我我应该如何比较这两个计数吗?
唯一的区别是列标题,如下图所示(左边是MYSQL,右边是MSSQL)
这是查询和结果
select count(userfieldid) from tr_userfield where calcexpression like '%@004%' and usefor like '%B%'
_____________________________________________
count(*) | (No Column Name)
_____________________________________________
43 | 43
|
这是我的 JAVA 代码
package a7.unittests.dao;
import static org.junit.Assert.*;
import java.sql.Connection;
import java.sql.ResultSet;
import org.junit.Test;
import java.sql.PreparedStatement;
import artemispm.autocalc.TRBaseScoreCalculator;
public class UnitTestTRBaseScoreCalculator_Select {
@Test
public void testQUERY_ISTHISCHARACINUSE() throws Exception{
char WILDCARD = '%';
UnitTestHelper helper = new UnitTestHelper();
Connection con = helper.getConnection(helper.sourceDBUrl);
Connection conTarget = helper.getConnection(helper.targetDBUrl);
PreparedStatement stmt = con.prepareStatement(String.format(TRBaseScoreCalculator.QUERY_ISTHISCHARINUSE_1, WILDCARD, "@004", WILDCARD, WILDCARD, "B", WILDCARD));
ResultSet sourceVal = stmt.executeQuery();
stmt = conTarget.prepareStatement(String.format(TRBaseScoreCalculator.QUERY_ISTHISCHARINUSE_1, WILDCARD, "@004", WILDCARD, WILDCARD, "B", WILDCARD));
ResultSet targetVal = stmt.executeQuery();
assertTrue(helper.resultSetsEqual2(sourceVal,targetVal)); }
}
package a7.unittests.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
public class UnitTestHelper {
public Connection getConnection(String url)throws Exception{
return DriverManager.getConnection(url);
}
/*
* Use for Insert and Update and Delete Statements
*/
public boolean resultSetsEqual4 (int source, int target){
System.out.println("The source is: "+source+" The target is: "+target);
if(((source>0)&&(target>0)&&(target==source)))
{
return true;
}
else
{
return false;
}
}
/*
* Used to test SELECT statements passing in objects
*/
public boolean resultSetsEqual2 (ResultSet source, ResultSet target) throws SQLException{
System.out.println("The source is: "+source+" The target is: "+target);
while(source.next())
{
target.next();
ResultSetMetaData metadata = source.getMetaData();
int count = metadata.getColumnCount();
for (int i =1; i<=count; i++)
{
if(!source.getObject(i).equals(target.getObject(i)))
{return false;}
}
}
return true;
}
public boolean resultSetsEqual (ResultSet source, ResultSet target) throws SQLException{
System.out.println("The source is: "+source+" The target is: "+target);
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;
}
public boolean resultSetsEqual3 (ResultSet rs1, ResultSet rs2) throws SQLException {
int col = 1;
//ResultSetMetaData metadata = rs1.getMetaData();
//int count = metadata.getColumnCount();
while (rs1.next() && rs2.next()) {
final Object res1 = rs1.getObject(col);
final Object res2 = rs2.getObject(col);
// Check values
if (!res1.equals(res2)) {
throw new RuntimeException(String.format("%s and %s aren't equal at common position %d",
res1, res2, col));
}
// rs1 and rs2 must reach last row in the same iteration
if ((rs1.isLast() != rs2.isLast())) {
throw new RuntimeException("The two ResultSets contains different number of columns!");
}
}
return true;
}
}