问题
I have one simple table in one Oracle database that needs to be joined with a group of tables in another Oracle database. They reside on the same server (different ports). I am using JDBC and want to keep it simple. I could connect to both DBs and join the result sets in Java. But I am wondering if there is a better/easier way.
I cannot easily use new tools or frameworks since I work in the rigid corporate environment, so want to know if it can be accomplished with just JDBC.
回答1:
No way to do it in pure JDBC as far as I am aware, but you could use the oracle databaselink facility. It makes the tables from one database available in another, allowing you to carry out joins etc as if they were in the same database. JDBC will work nicely with tables that are subject to these links.
Setting them up is an administrative function, so you'll need some DBA involvement.
http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_5005.htm
Other than that, if the "one" table isn't too big, you may have to read that to a Map and then peform the join parts of the query in your code ( which is not ideal )
回答2:
I see two options.
- Get the data from two datasources manually and then combine it(which you have already thought about).
- Create a db link from one database to other database and create a synonym in your schema for the remote object and directly execute the sql statement with joins from java.
来源:https://stackoverflow.com/questions/18896524/join-data-from-2-oracle-tables-located-in-different-database-on-the-same-server