5

I have two local databases I'm trying to connect to using Java's Connection class. It's easy enough to connect to the first database using:

public Connection conn;
conn = DriverManager.getConnection(connectionString);

How can I add a second database to that same connection? They're both on the same server so it should be fairly simple but I can't find the right commands to do it.

Thanks

4
  • why do you want to connect two databases from the same connection? to replicate? Commented Oct 25, 2011 at 7:44
  • So that I can run transactions that query both databases Commented Oct 25, 2011 at 8:40
  • say if you have table1 in db1 and table2 in db2, so you need to run queries joining table1 and table2? Commented Oct 25, 2011 at 9:05
  • More like, I have table1 in db1 with columns id1 and id2, and table2 in db2 with columns id1 and id3, and I want to update id3 in table2 conditional on id2 having certain values in table 1 where t1.id2 = t2.id2 Commented Oct 25, 2011 at 9:33

4 Answers 4

9

A Connection is a session with a specific database. You can't use one Connection to communicate with two different databases; for that, you need two separate Connections.

Connection conn1 =  DriverManager.getConnection(connectionString1);
Connection conn2 =  DriverManager.getConnection(connectionString2);
Sign up to request clarification or add additional context in comments.

1 Comment

That's the workaround I was using but I was hoping there would be a better way to do it
2

Have you tried:

public Connection conn1;
conn1 = DriverManager.getConnection(connectionString1);
public Connection conn2;
conn2 = DriverManager.getConnection(connectionString2);

Comments

1
  1. Instance members shouldn't be public.

  2. Connection should be a local variable, not an instance member.

You can only connect to one database at a time with a single Connection. Ergo you need another Connection.

2 Comments

Thanks, but I was already following 1 and 2 in my code, I'd just simplified it for the example here.
@Zain I don't see how that simplifies anything. Actually it just wastes your time and mine.
0

I think you have to use J2EE, JTA transaction manager to accomplish this.

4 Comments

Using 2 connections is not very transaction safe. With JTA transaction manager it is possible to achieve this functionality in transparent manner,stackoverflow.com/questions/1961566/…
I would have thought using two connections wasn't transaction-safe at all, but that statement isn't the same thing as 'you have to use'. OP has said nothing about transaction safety across the databases being a requirement, nor about being in a J2EE environment where JTA is even available.
since it says, 'How can I add a second database to that same connection', using of two connections does not seems to be the option as I think. So what left is to access databases in transaction safe manner that is why i suggested to use JTA, as I am not aware of any other mechanism to achieve this.
The other mechanism is to rejectb the terms of the question, as everyone else has done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.