How to compare the results of two databases?

I have two databases, and I need to compare three rows from each database, if they match, I need to print this row.

Example:

Database 1:
RuleId  Dataset  Partition  Date
------  -------  --------   ----
1234     ABVD    012145    21/01/2014
1256     ABCD    124565    22/01/2013
4567     FGHJ    0123456  22/02/2013
Database 2:
1234    ABCD    012345    21/01/2014
4567    FGHJ    0123456   22/02/2013
7894    MNBV    0147896   20/01/2014

If RuleId, Dataset and Partition are equal in both databases, I have to print these lines in the output.

My code is as follows:

ArrayList<String> rslist = new ArrayList<String>();
ArrayList<String> rs1list = new ArrayList<String>();
int count = 1;
while (rs.next()) {
    int i = 1;
    count = 1;
    while (i < count) {
        rslist.add(rs.getString(i++));
    }

    rslist.add(rs.getString(2) + rs.getString(4) + (rs.getString(5)));

}
int count1 = 1;
while (rs1.next()) {
    int i = 1;
    count1 = 1;
    while (i < count1) {
        rs1list.add(rs1.getString(i++));

    }
    rs1list.add(rs1.getString("RuleId") + rs1.getString("Dataset")
            + rs1.getString("Partition"));
}
for (String s1 : rslist)
    for (String s2 : rs1list)
        if (s1.equals(s2)) {

        }

If these lines are equal, I have to print all the values ​​of the lines.

+3
source share
4 answers

ResultSet is a subclass of Object in the long run, and we can compare two or more objects directly. A little about your question:

ResultSet rs=st.executeQuery(sql1);
ResultSet rs1=st.executeQuery(sql2);

where sql1 and sql2 are 2 statements

while(rs.next() && rs1.next()){
         int a=rs.next();
         int b=rs1.next();
         if(a==b){
              System.out.println("Compairing two ResultSets");
         }
     }
0
source

In your method, executeQuery()you can use the following SQL command:

select * from db1,db2 where db1.ruleid=db2.ruleid and db1.dataset=db2.dataset and db1.partition=db2.partition;

, .

+1

( ) :

    Connection con1 = DriverManager.getConnection("jdbc:mysql://localhost/db1", "user", "pass");
    Connection con2 = DriverManager.getConnection("jdbc:mysql://localhost/db2", "user", "pass");

    Statement stmt1 = con1.createStatement();
    Statement stmt2 = con2.createStatement();

    ResultSet rs1 = stmt1.executeQuery("select * from ... order by ... ");
    ResultSet rs2 = stmt2.executeQuery("select * from ... order by ... ");

    while(rs1.next() && rs2.next()) {
        // compare basically rs1.getObject(i).equals(rs2.getObject(i))
    }

db1, witk PK db2. ...

0

, .

equals,

public class Record {
    ...
    public boolean equals(Object record) {
        ...

Set ( ArrayList, , LinkedHashSet, ), , , ..

Other proposed approaches involve very well-formed sets with a clear order, without duplication, without intermediate inserts (A, C against A, B, C), etc.

0
source

All Articles