【问题标题】:Comparison between two ResultSet in JavaJava中两个ResultSet的比较
【发布时间】:2011-10-19 02:12:46
【问题描述】:

处理 Java ResultSet 的最佳方法是什么?我正在创建一个桌面应用程序,它将使用 JDBC 连接到 Oracle 数据库。

但是,我在处理 ResultSet 时遇到了问题,因为我无法使用 for 循环进行比较。

// create a database connection object
DB db = new DB();

// get rows of my first table and second table
ResultSet firstRS = db.getMyFirstTable();
ResultSet seconRS = db.getSecondTable();

// compare the two tables
while(firstRS.next()) 
{
    // get the row of my first table
    String firstRSRow = firstRS.getString("col1");

    while(secondRS.next()) 
    {
        // get the row of my second table
        String secondRSRow = seconRS.getString("col1");

        // compare row from first table to the row in second table
        if(firstRSRow.startsWith(secondRSRow))
        {
            // do some processing here....
        } // end if
    } // end while
} // end while

我知道这可以通过 Hibernate 几行代码来完成,但我没有时间研究它。

我还从 apache 中找到了 commons DbUtils,但对于新手程序员来说这似乎很复杂。

是否有其他方法/工具/库/框架足够简单,可以从数据库中获取 ResultSet 并以简单直接的方式对其进行操作?

如果您能指导我访问一个包含有关 java 数据库连接的示例代码的网站,我将不胜感激。

非常感谢您的支持。

【问题讨论】:

    标签: java database oracle hibernate jdbc


    【解决方案1】:

    为什么是ResultSet?您可以轻松地将值与SELECT 语句进行比较。如果您仍想使用 ResultSet,请考虑 CachedRowSet

    【讨论】:

    • @user692533 - 最好为特定列填充 List 并比较它们。
    【解决方案2】:
    DB db = new DB();
    
     // get rows of my first table and second table
    ResultSet firstRS = db.getMyFirstTable();
    ResultSet seconRS = db.getSecondTable();
    
    // compare the two tables
    while(firstRS.next() || secondRS.next()) 
    {
    // get the row of my first table
    String firstRSRow = firstRS.getString("col1");
    String secondRSRow = seconRS.getString("col1")
    
        // compare row from first table to the row in second table
        if(firstRSRow.startsWith(secondRSRow))
        {
            // do some processing here....
        } // end if
    } // end while
    

    【讨论】:

      【解决方案3】:
      public static boolean resultset(String SQL1, String SQL2){
          public boolean status=false;
          ResultSet ViewResultset = st.executeQuery(SQL1);
            ResultSet QueryResultset = st.executeQuery(SQL2);
      
            while (QueryResultset.next() | ViewResultset.next())
            if (ViewResultset.getRow() == 0   | QueryResultset.getRow() == 0) {
            break;
            } else {
               for (int i = 1; i < ViewResultset.getMetaData().getColumnCount() + 1; i++) {
                  System.out.println("OOO"+ QueryResultset.getMetaData().getColumnCount());
                  String columnName = ViewResultset.getMetaData().getColumnName(i);
            System.out.println("ColumnName :" + columnName);
          for (int j = 1; j < QueryResultset.getMetaData().getColumnCount() + 1; j++)
          if (ViewResultset.getMetaData().getColumnName(i).equals(QueryResultset.getMetaData().getColumnName(j))&& !(QueryResultset.getString(columnName) == null || ViewResultset.getString(columnName) == null))
          if (ViewResultset.getString(columnName).toUpperCase().contains(QueryResultset.getString(columnName).toUpperCase())) {
              System.out.println(" Actual "+ ViewResultset.getMetaData().getColumnName(i)     + " Expected: "+ ViewResultset.getString(columnName));
              status=true;
      
          }      
          else {System.out.println(" Actual "+ ViewResultset.getMetaData().getColumnName(i)     + " Expected: "+ ViewResultset.getString(columnName));
          }
          }
          }
          return status;
          } 
      }
      

      【讨论】:

        【解决方案4】:

        ResultSet 最终是 Object 的子类,我们可以直接比较两个或多个对象。有点具体到你的问题:

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

        其中 sql1 和 sql2 是 2 个语句

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

        【讨论】:

        • rs.next() 返回布尔值而不是整数
        • #石兰。你疯了吗?。我正在分享做的方式。不适用于编码语句。
        • 我相信你还没有意识到 rs.next() 的概念。当您检查 rs.next() 时,您已经将指针移动到记录前面,再次调用它,您将指针向前移动并跳过第一条记录!此外,如果您想比较 rs1 和 rs!!您应该知道它们是对象,不能像 == 那样进行比较。基本上我在这段代码中看不到任何正确的陈述!
        • #石兰。你以为你这么聪明吗?可能你是个盲人。如果您认为自己很聪明,请给出这个问题的答案。不要指向他人。
        • 我很聪明,当我对此一无所知时不会回答:D
        猜你喜欢
        • 2013-01-15
        • 1970-01-01
        • 2011-09-18
        • 1970-01-01
        • 2012-12-12
        • 2016-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多