Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

selecting cross - database servers in java

Posted on 2006-10-24
8
Medium Priority
?
289 Views
Last Modified: 2006-11-28

hi all

can anyone show me a way around my problem?

i have 2 database servers, postgresql and mysql

i need to do a select query on the postgresql database and use the recordset set returned to do another select on a mysql database

something like this

select id from postgresqltable inner join mysql table

is this possibel.  i able to connect using jdbc to both database but i don't know how to use the select result from the postgresql table and join them with the mysql table

any help greatly appreciated
0
Comment
Question by:dalecon
8 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 136 total points
ID: 17794728
Just use two queries and join them 'manually'
0
 

Author Comment

by:dalecon
ID: 17794793
if i use this


// go off to postgresql
conn = Connection.getConnection();
PreparedStatement ps = conn.prepareStatement(sql, rsPS.TYPE_SCROLL_INSENSITIVE, rsPS.CONCUR_READ_ONLY);
rsPS = ps.executeQuery();

// go off to mysql
conn = ConnectionMySQL.getConnection();
PreparedStatement ps = conn.prepareStatement(sql, rsMS.TYPE_SCROLL_INSENSITIVE, rsMS.CONCUR_READ_ONLY);
rsMS = ps.executeQuery();


How do 'join' the 2 recordset?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17794808
Get the key field from the first and use it to lookup in the 2nd recordset
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 92

Assisted Solution

by:objects
objects earned 132 total points
ID: 17794884
loop thru the first result set, and for each record make a select on the other database by the the foreign key

conn = Connection.getConnection();
PreparedStatement ps = conn.prepareStatement(sql, rsPS.TYPE_SCROLL_INSENSITIVE, rsPS.CONCUR_READ_ONLY);
rsPS = ps.executeQuery();
while (rsPS.next()) {

   Object key = rsPS.getObject(...);
   String sql = ...

   // go off to mysql
   conn = ConnectionMySQL.getConnection();
   PreparedStatement ps = conn.prepareStatement(sql, rsMS.TYPE_SCROLL_INSENSITIVE, rsMS.CONCUR_READ_ONLY);
   rsMS = ps.executeQuery();

   // loop thru rsMS
}
0
 

Author Comment

by:dalecon
ID: 17794893
no the query is too complex it's full of left outer, inner and union joins. with a massive where clause using an old version of mysql so i can do any sub queries

i also need the results as a recordset and send as xml using jdom

i'll have to copy the data in a sql job from one server to the other and do it that way

0
 
LVL 26

Assisted Solution

by:Tomas Helgi Johannsson
Tomas Helgi Johannsson earned 132 total points
ID: 17795421
   Hi!

You should use this method as CEHJ says : http://java.sun.com/j2se/1.5.0/docs/api/javax/sql/rowset/JoinRowSet.html
Example (taken from the above link) :

    JoinRowSet jrs = new JoinRowSetImpl();
 
     ResultSet rs1 = stmt.executeQuery("SELECT * FROM EMPLOYEES");
     CachedRowSet empl = new CachedRowSetImpl();
     empl.populate(rs1);
     empl.setMatchColumn(1);
     jrs.addRowSet(empl);
 
     ResultSet rs2 = stmt.executeQuery("SELECT * FROM ESSP_BONUS_PLAN");
     CachedRowSet bonus = new CachedRowSetImpl();
     bonus.populate(rs2);
     bonus.setMatchColumn(1); // EMP_ID is the first column
     jrs.addRowSet(bonus);

Regards,
  Tomas Helgi
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question