Solved

selecting cross - database servers in java

Posted on 2006-10-24
8
280 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 34 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 92

Assisted Solution

by:objects
objects earned 33 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 25

Assisted Solution

by:Tomas Helgi Johannsson
Tomas Helgi Johannsson earned 33 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now