I have 2 different databases. Actually the second database is with all the records that are identical to the first one. Mean when i create/delete/update any entry in the first database, then after a certain while it is visible in the second database table as well. Although there are some minor differences as well like two rows are different or additional in the first table.
My question is how to compare these two tables from two different databases in java.
I connect to the databases like this but i don't know how to proceed further. Any help would be appreciated
String SQL_Statement = "SELECT * FROM Table_All_Customers";
for (Connection con : getDBConnections())
{
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(SQL_Statement);
int count=0;
int rowCount= 0;
while (rs.next())
{
count+=1;
rowCount+=1;
String productNumber = rs.getString("Customer");
System.out.println("This is the result ... " + rowCount + " :" + productNumber);
}
}
With the above code, i am just fetching the data from the columns of these tables and printing them
the tables look like this
table 1 in database 1:
UserID Customer Order number
----------------------
2334e3 David Pizza 2
2334e4 David Cola 3
2334e5 Lisa Cola 4
2334e6 Lina Pizza 5
2334e7 Greco Cola 6
table 1 in database 2:
UserID Customer Order number
----------------------
2334e3 David Pizza 2
2334e4 David Cola 3
2334e5 Lisa Cola 4
2334e6 Lina Pizza 5
My aim is to compare these two tables above to check if data is updated successfully in two different databases. Also is there any way to check the differences as well? How to achieve it via Java. Would be thankful for any help.
Updated:
I tried something like this but i am unable to compare the differences, is it possible to print the differences between two tables?
public static boolean isDBSame()
{
try
{
Connection con = DriverManager.getConnection(URL, userName, pass);
Connection con2 = DriverManager.getConnection(URL2, userName, pass);
Statement stmt = con.createStatement();
Statement stmt2 = con2.createStatement();
ResultSet rs = stmt.executeQuery(SQL_Statement);
ResultSet rs2 = stmt2.executeQuery(SQL_Statement_2);
int count = 0;
int rowCount = 0;
int firstDB_count = 0;
int firstDB_rowCount = 0;
List<String> tags = new ArrayList<String>();
List<String> tags2 = new ArrayList<String>();
List<String> tags3 = new ArrayList<String>();
Set<String> ad = new HashSet<String>(tags);
Set<String> bd = new HashSet<String>(tags2);
while (rs.next())
{
String Customer = rs.getString(1);
System.out.println("This is the content of 1st database: " + rowCount + " : " + Customer.toString());
count += 1;
rowCount += 1;
tags.add(rs.getString("UserID"));
while (rs2.next())
{
String Customer2 = rs2.getString(1);
tags2.add(rs2.getString("CUserID"));
}
}
for (String e : tags2)
{
System.out.println("This is the second database e.... " + e);
}
if (tags.equals(tags2))
{
System.out.println("Hi ");
System.out.println("Content is same...");
return true;
}
else
{
System.out.println("Content is not same and check properly....... " );
}
}
catch (Exception e)
{
System.out.println("It is false...");
System.out.println(e.toString());
}
return false;
}
Even i tried this
Set<String> ad = new HashSet<String>(tags);
Set<String> bd = new HashSet<String>(tags2);
ad.removeAll(bd);
But it result in false or true. Is there anyway that i can print the difference between two lists? Also any efficient way to compare different columns of two different tables in the 2 different databases.
You could structure your code a bit better. For example by breaking down what you are trying to achieve into logical steps. It works better than trial-and-error development:
Something like this (untested):
I am assuming here that it is ok to compare the String representation of your column values, which should be ok for most cases.
The statements like
equals = equals && (rowCount == getRowCount(resultSet2))
are making sure the existing value of equals is not overwritten. To assure that once equals becomes false, subsequent checks will not make it true again.The part
rowCount == getRowCount(resultSet2)
is a boolean expression which evaluates totrue
if the two rowCounts are equal and false otherwise.You need to create the
Database
class yourself, containing the fields needed to connect to the database (url, userName, password).The SQL query needs to sort the rows by UserId, so:
Otherwise the order of the results is unspecified and comparing them sequentially might fail.