2011-10-19 32 views
2

Java ResultSetを処理する最善の方法は何ですか? JDBCを使用してoracleデータベースに接続するDesktopアプリケーションを作成しています。Javaの2つのResultSetの比較

しかし、私はforループを使用して比較を行うことができないので、ResultSetsの処理に問題があります。

// 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で達成することができることを知っているが、私はそれを勉強する時間の贅沢を持っていません。

私はまた、コモンDbUtilsをApacheから見つけましたが、初心者のプログラマーとして私にとっては複雑です。

データベースからResultSetを取得し、それを簡単かつ直接的に操作するのに十分な、他の方法/ tools/libraries/frameworkがありますか?

Javaデータベースの接続に関するサンプルコードのあるWebサイトに移動できるかどうかをご理解いただけます。

ご協力いただきありがとうございます。

答えて

0

なぜResultSetSELECTステートメントで簡単に値を比較することができます。あなたがまだResultSetを望む場合には、CachedRowSetを考えてください。

+0

ある

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

- 特定の列のリストを移入し、それらを比較することをお勧めします。 – adatapost

0
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 
-1

ResultSetは最終的にObjectのサブクラスであり、2つ以上のオブジェクトを直接比較できます。あなたの質問にはほとんど特定:SQL1とSQL2は、2つの計算書user692533 @

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

rs.next()はbooleanを返します。 – Shilan

+0

#Shilanです。ばかじゃないの?。私は道を分かち合っていました。コーディングステートメントではありません。 –

+0

私はあなたがrs.next()の概念を実現していないと信じています。 rs.next()がすでにレコードの前にポインタを移動しているかどうかをチェックするときに、ポインタをもう一度呼び出すことによってポインタを前方に移動し、最初のレコードをスキップします。さらに、rs1とrsを比較したい場合は!!それらはオブジェクトであり、==のように比較することはできません。基本的に私はこのコードで正しい記述を見ません! – Shilan

0
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; 
    } 
}