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?
source to share
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.
source to share