Compare Data Without Specifying the Columns

Posted on the 12 August 2019 by Asik Ali @Asikali077

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;
}