How to create multiple database connections for different databases in java

Harsha picture Harsha · May 15, 2012 · Viewed 39.1k times · Source

I have an application which uses four databases in different geographical locations. All the databases contains same tables and only the database name is different according to the location. I have to create some reports in my application which uses data from each database. What would be the proper way to create those database connection from a java application and is there a suitable design pattern for this task which I could use?

Answer

Adeel Ansari picture Adeel Ansari · May 15, 2012

As you have not tagged your question with any of this, hibernate, JPA, ORM, I assume you are dealing with plain JDBC.

Having said that, I suggest you to have a DAO layer to deal with underlying databases, and leave the connection details to specific implementations. You can configure your connection strings in some .properties files, lets say.

[Complement]

You can also make use of DAO factory, an implementation of Abstract Factory or Factory Method pattern, whichever suits here.

[Links]