Compare Data without Specifying the Columns

Posted on

private static boolean getDataFromDB() {
try {

String srcTableQuery = getSrcQuery();
String tgtTableQuery = getTgtQuery();

ResultSet srcResultSet = srcStatement.executeQuery(srcTableQuery);
ResultSet tgtResultSet = tgtStatement.executeQuery(tgtTableQuery);

boolean status = false;
while (srcResultSet.next() | tgtResultSet.next())
if (tgtResultSet.getRow() == 0 | srcResultSet.getRow() == 0)

{
break;
} else {
for (int i = 1; i < tgtResultSet.getMetaData().getColumnCount() + 1; i++) {
String columnName = tgtResultSet.getMetaData().getColumnName(i);
for (int j = 1; j < srcResultSet.getMetaData().getColumnCount() + 1; j++)
if (tgtResultSet.getMetaData().getColumnName(i)
.equals(srcResultSet.getMetaData().getColumnName(j))
&& !(srcResultSet.getString(columnName) == null
|| tgtResultSet.getString(columnName) == null))
if (tgtResultSet.getString(columnName).toUpperCase()
.contains(srcResultSet.getString(columnName).toUpperCase())) {
System.out.println(
“Records matched : Actual ” + srcResultSet.getMetaData().getColumnName(i)
+ ” ” + srcResultSet.getString(columnName) + ” Expected: ”
+ tgtResultSet.getMetaData().getColumnName(i) + ” ”
+ tgtResultSet.getString(columnName));
status = true;
} else {
System.out.println(“Records Not matched : Actual ”
+ srcResultSet.getMetaData().getColumnName(i) + ” ”
+ srcResultSet.getString(columnName) + ” Expected: ”
+ tgtResultSet.getMetaData().getColumnName(i) + ” ”
+ tgtResultSet.getString(columnName));
}
}
}
return status;

} catch (Exception e) {
System.out.println(“Exception thrown while comparing data”);
e.printStackTrace();
}
return false;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s