开发者

how to iterate for all values of table1( using Java)?

开发者 https://www.devze.com 2023-02-02 06:20 出处:网络
how to iterate for all values of table1( using java)? say, table1 has values: pkeynew_id<fkey>old_id<fkey>

how to iterate for all values of table1( using java)?

say, table1 has values:
pkey  new_id<fkey>  old_id<fkey>
====================================
1 20  1787
2 24  1789
3 29  1793

From above table pkey say,for id=1 i've retrieved values from two respective tables which are pkey's in other two tables say tab2,tab3. :select new_id,old_id from tab1; assigned to object;

tab2
----
new开发者_如何学Python_id(pkey)  values
====================
20  yu
32  ty
23  nm
24  to

tab3
----
old_id(pkey)   values
=====================
1780  ghgjj
1785  fhfhj
1787  fdgfh
1789  fjhgj
1793  fjjg

i was able to retrieve for id=1,

"select values from tab2 where newid=+new_id(object)+"

"select values from tab2 where newid=+old_id(object)+"

how to iterate for all values of table1? Tables are from different databases. olddatabase1: tab3 newdatabse1: tab1,tab2; tab1 is mapping table for old_id and new_id.


You can iterate the resultset (select * from table) with this way. from JDBC Wikipedia article

Statement stmt = conn.createStatement();
try {
ResultSet rs = stmt.executeQuery( "SELECT * FROM MyTable" );
try {
    while ( rs.next() ) {
        int numColumns = rs.getMetaData().getColumnCount();
        for ( int i = 1 ; i <= numColumns ; i++ ) {
           // Column numbers start at 1.
           // Also there are many methods on the result set to return
           //  the column as a particular type. Refer to the Sun documentation
           //  for the list of valid conversions.
           System.out.println( "COLUMN " + i + " = " + rs.getObject(i) );
        }
    }
} finally {
    try { rs.close(); } catch (Exception ignore) { /* Propagate the original exception
  instead of this one that you may want just logged */ }
}
} finally {
try { stmt.close(); } catch (Exception ignore) { /* Propagate the original exception
instead of this one that you may want just logged */ }
}


First, you want to join the tables together using:

select pkey, t2.values as t2vals, t3.values as t3vals 
from table1 t1, table2 t2, table3 t3 
where t1.new_id = t2.new_id and t1.old_id = t3.old_id

Now, when you iterate over that result set you will get all the values for each row.

0

精彩评论

暂无评论...
验证码 换一张
取 消