How can I use multiple JDBC drivers in one application?

As far as I understand, as soon as I execute

Class.forName("net.sourceforge.jtds.jdbc.Driver");

      

I am initializing an application to use the JTDS SQL Server driver globally and

java.sql.DriverManager.getConnection(url, user, password);

      

returns SQL Server connections after that.

But what if I want to work with several different database engines in the same function, getting a JTDS SQL Server connection and then for example a PostgreSQL connection and then a new JTDS SQL Server connection?

+3


source to share


2 answers


You are misunderstanding. When you load a driver class with Class.forName()

, that driver is registered with the Driver Manager. You can do this with as many drivers as possible.

The first parameter getConnection()

is a URL that uniquely identifies the driver to use for this connection.



However, instead of fetching connections directly from the driver manager, I recommend that you use a connection pool (like Apache DBCP ). This will allow you to retrieve connections as needed and will provide additional functionality such as a warning if you forgot to return a pool connection.

+7


source


You need to use DataSource

. Configure DataSource

for each connection type and use the appropriate one each time DataSource

(e.g. via the correct one DAO

)



+1


source







All Articles