java 如何在java中比较两个结果集的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15184100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:45:10  来源:igfitidea点击:

How to compare the values of two resultset in java

javaresultset

提问by

I have two tables. And these tables have the same schema consisting of userid, username. I want to check is there any common usernamein table1and table2.

我有两张桌子。这些表具有相同的架构,由用户 ID、用户名组成。我想看看是否有任何共同usernametable1table2

 rs1 = statement.executeQuery("select username from table1")
 rs2 = statement.executeQuery("select username from table2")

My logic is:

我的逻辑是:

  • while(rs1.next())
  • compare the value of rs1with every value of rs2.
  • Ifthere match found print one of the value elseprint both the values.
  • while(rs1.next())
  • 将 的值rs1与 的每个值进行比较rs2
  • If有匹配找到打印值之一else打印两个值。

Is there a way to achieve this in java... Please anyone help me ... Thanks...

有没有办法在java中实现这一点......请任何人帮助我......谢谢......

采纳答案by NPE

I would use a single SQL statement:

我将使用单个 SQL 语句:

select table1.username from table1, table2 where table1.username = table2.username

This will only return usernames that appear in both tables, so no post-processing will be needed.

这将只返回出现在两个表中的用户名,因此不需要后期处理。

This construct is called an inner join. If you also want to identify usernames that are unique to table1and/or table2, you could use an outer join.

此构造称为内部联接。如果您还想识别table1和/或唯一的用户名table2,您可以使用外连接

回答by Khaled.K

Pseudo-Code:

伪代码:

if ( A.type = B.type )
{
    PRINT same type

    if ( A.format = B.format )
    {
        PRINT same format

        if ( A.value = B.value )
        {
            PRINT same value
        }
        else
        {
            PRINT different value
        }
    }
    else
    {
        PRINT different format
    }
}
else
{
    PRINT different type
}

回答by Sarath Kumar Sivan

You can either solve it through SQL statement IN and NOT IN or you can try something like this:

您可以通过 SQL 语句 IN 和 NOT IN 解决它,也可以尝试这样的操作:

public boolean compareResultSets(ResultSet resultSet1, ResultSet resultSet2) throws SQLException{
        while (resultSet1.next()) {
            resultSet2.next();
            ResultSetMetaData resultSetMetaData = resultSet1.getMetaData();
            int count = resultSetMetaData.getColumnCount();
            for (int i = 1; i <= count; i++) {
                if (!resultSet1.getObject(i).equals(resultSet2.getObject(i))) {
                    return false;
                }
            }
        }
        return true;
    }

回答by Akash5288

I am giving an example to solve this:

我举一个例子来解决这个问题:

rs1 = statement.executeQuery("select username from table1")
rs2 = statement.executeQuery("select username from table2")

while(rs1.next()) {
// Compare till rs1 reachs its last record.
  while(rs2.next()) {
     if() {
       // Do your code here...
     }
  }

// this will move resultSet cursor to the first position.
rs2.first();
}

回答by vishal ajwani

One way could be:-

一种方法可能是:-

String query = "(" + query1 +") intersect ("+ query2 + ")";

Where in the intersect operation will anyway give you the common columns.

无论如何,相交操作中的何处都会为您提供公共列。

P.S. :- I know this question is old, but it might help somebody.

PS :- 我知道这个问题很老,但它可能对某人有所帮助。