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
To connect to multiple databases in a single JDBC program you need to connect to the two (or more) databases simultaneously using the above steps. Here, in this example, we are trying to connect to Oracle and MySQL Databases where following are the URLs and sample user credentials of both databases.
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);
Have you tried:
public Connection conn1;
conn1 = DriverManager.getConnection(connectionString1);
public Connection conn2;
conn2 = DriverManager.getConnection(connectionString2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With