如何在Java中为不同的数据库创建多个数据库连接 [英] How to create multiple database connections for different databases in java
问题描述
我有一个使用在不同地理位置的四个数据库的应用程序.所有数据库都包含相同的表,仅数据库名称根据位置而有所不同. 我必须在应用程序中创建一些报告,这些报告使用每个数据库中的数据.从Java应用程序创建那些数据库连接的正确方法是什么?我可以使用适合此任务的合适的设计模式吗?
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?
推荐答案
由于尚未使用hibernate
,JPA
,ORM
中的任何一个标记问题,因此我假设您正在处理纯JDBC.
As you have not tagged your question with any of this, hibernate
, JPA
, ORM
, I assume you are dealing with plain JDBC.
话虽如此,我建议您有一个DAO层来处理基础数据库,并将连接详细信息留给特定的实现.可以说,您可以在某些.properties
文件中配置连接字符串.
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.
[补码]
您还可以使用DAO工厂,这是抽象工厂或工厂方法模式的实现,适用于此处.
You can also make use of DAO factory, an implementation of Abstract Factory or Factory Method pattern, whichever suits here.
[链接]
- DAO和DAO Factory的非常好实现,通过 BalusC
- 核心J2EE模式-可以过时,但可能会提供一些想法.
- A very fine implementation of DAO and DAO Factory, by BalusC
- Core J2EE Patterns -- arguably dated but might provide some idea.
这篇关于如何在Java中为不同的数据库创建多个数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!