Java中两个ResultSet的比较

发布于 2024-12-10 23:14:56 字数 1203 浏览 0 评论 0原文

处理 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 但对于我作为一个新手程序员来说这似乎很复杂。

是否有其他足够简单的方法/工具/库/框架可以从数据库获取结果集并以简单直接的方式操作它?

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

非常感谢您的支持。

What is the best way to handle Java ResultSet? I'm creating a Desktop application that will connect to an oracle database using JDBC.

However, I am having problem with handling ResultSets since I can't do comparison using for loop.

// 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

I know that this could be accomplish with Hibernate with a few lines of code but I don't have the luxury of time to study it.

I also found commons DbUtils from apache but it seems complicated for me as a novice programmer.

Is there other way/tools/libraries/framework simple enough to get ResultSet from databases and manipulate it in simple and straightforward manner?

I will also appreciate if you could direct me to a website that has sample codes concerning java database connectivity.

Thank you very much for your support.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

与他有关 2024-12-17 23:14:56

为什么结果集?您可以轻松地使用 SELECT 语句比较值。如果您仍然想要 ResultSet,请考虑 CachedRowSet

Why ResultSet? You could easily compare values with SELECT statement. In case you still want to ResultSet then think about CachedRowSet.

谈下烟灰 2024-12-17 23:14:56
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
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
漆黑的白昼 2024-12-17 23:14:56
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;
    } 
}
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;
    } 
}
友谊不毕业 2024-12-17 23:14:56

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

 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");
     }
 }

ResultSet is a subclass of Object ultimately and we can compare two or more objects directly. A little specific to 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");
     }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文