【问题标题】:How to compare 2 database tables using Java如何使用 Java 比较 2 个数据库表
【发布时间】:2020-02-05 09:21:28
【问题描述】:

我这里有一个场景,我需要使用 java 比较 2 个数据库表

第一个数据库表有

id  Name Salary
1   ABC  1000
2   XYZ  2000
3   LMN  3000

第二个数据库表有

id Name Salary
4  PQR  5000
2  XYZ  2000
1  ABC  4000
3  LMN  2500

我曾使用 list 进行比较,但有没有其他方法可以使用 selenium java 比较第一个表和第二个表。

我希望输出是

Second table has 4 rows while 1 table has 3 rows (or vice versa)
1st table LMN salary is 3000 and second table LMN salary is 2500

【问题讨论】:

  • Selenium 和数据库之间没有连接。
  • 我已经使用 JDBC 在 selenium 和数据库之间进行连接。我也能够从数据库中检索数据。只需要建议来比较使用 selenium 的数据库表
  • Selenium 是连接基于 html 的 UI。你不能用它来处理数据库。 JDBC 是连接 Java 和 DB,而不是 Selenium 和 DB。
  • 对不起,您可以纠正这个问题
  • @gurioso 作者声称他是用硒做的,但他没有。代码中没有任何内容来自 Selenium API。称它为 driver 并不能使它成为 Selenium。

标签: java database jdbc


【解决方案1】:

我会直接在 SQL 中进行请求的比较。这会更快。根据您需要的结果,有几种方法可以做到这一点。

由于您使用的是 Microsoft SQL,您可以通过执行以下操作以请求的格式检索行数:

SELECT 'The FirstTable has ' + cast((SELECT count(*) from FirstTable) as varchar(50)) + ' records, while the SecondTable has ' + cast((SELECT count(*) from SecondTable) as varchar(50)) + ' records';

要获得表之间的差异,您可以执行以下操作:

SELECT * FROM FirstTable
UNION 
SELECT * FROM SecondTable
EXCEPT 
SELECT * FROM FirstTable
INTERSECT
SELECT * FROM SecondTable;

然后你可以获取 ResultSet 并像这样比较条目:

while (resultSet.next())
{

  int id= rs.getInt("id");
  String name= rs.getString("name");
  int firstTableSalary = rs.getInt("salary");

  rs.next();
  int secondTableSalary = rs.getInt("salary");

  console.log(String.format("The first table salary is %d, while the second table salary is %d, name=%s, id=%d", firstTableSalary, secondTableSalary, name, id);

}

我准备了 here 一个用于检查 SQL 的小提琴。

【讨论】:

  • 我是测试人员,我想运行这些场景来显示两个表之间的区别。我正在使用带有 java 的 selenium cucumber 框架
  • @Suma 上面的代码可以和 Selenium 一起使用。你需要把它放在一个 WebDriver 脚本中
  • 是的,知道了。非常感谢
【解决方案2】:

使用 SQL 更容易获得工资差异

String query = "select t1.name,t1.salary t1_salary, t2.salary t2_salary" +
    " from table1 t1, table2 t2 "+
    " where t1.id=t2.id and t1.name=t2.name and t1.salary!=t2.salary";
//this will give you a result set of all the names and salaries from the two tables 
//where the salaries differ. 
Connection conn = ...
try (Statement stmt = conn.createStatement()){
    ResultSet rs = stmt.executeQuery(query);
    while (rs.next()){
        String name = rs.getString(1);
        Integer t1Salary = rs.getInt(2);
        Integer t2Salary = rs.getInt(3);
         //do something with these values
    }
    rs.close();

同样,通过查询捕获表长度

    int t1Length = 0;
    int t2Length = 0;
    rs = stmt.executeQuery("select count(*) from table1");
    while (rs.next()){
        t1Length = rs.getInt(1);
    }
    rs.close();
    rs = stmt.executeQuery("select count(*) from table2");
    while (rs.next()){
       t2Length = rs.getInt(1);
    }
    rs.close();
     //do something with the lengths
}
conn.close();

【讨论】:

  • 我有 100 条记录要比较。我无法逐一检索和比较。就像薪水一样,我在两个表中还有 6 列要比较
  • 表之间比较的列集是否总是相同的?
  • 是的,两个表中的列总是相同的
猜你喜欢
  • 2011-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-23
  • 1970-01-01
  • 2013-07-20
  • 1970-01-01
相关资源
最近更新 更多