我是否可能会同时遇到类路径中可用的两个驱动程序的任何问题?
Unlikely. The DriverManager.getConnection
method actually delegates the construction of the connection to all drivers registered with it. Only drivers that recognize the protocols in the JDBC URL will return the connection. The JDBC specification states:
When the DriverManager
is trying to
establish a connection, it calls that
driver’s connect method and passes the
driver the URL. If the Driver
implementation understands the URL, it
will return a Connection
object;
otherwise it returns null
.
...
The format of a JDBC URL is :
jdbc:<subprotocol>:<subname>
In the case of both jTDS and the Oracle (thin) driver, the protocol formats are different, and hence, you would never experience a problem. However, remember not to place more than one version of the same driver.
Is this a reasonable approach or is there a better design/pattern for this?
You are looking for a DataSource
. DataSources would have been availble in a Java EE environment, and not in Java SE applications. You can however, build your own DataSource or a similar class; you don't need to implement the DataSource interface itself, but you could do something similar. In your context, the ConnectionManager
class of yours will assume the role of the DataSource by possibly accepting a parameter that distinguishes which database to connect to; you could think about using a connection pool in case you need one (unlikely if you need only one connection to the database).
You could also adopt @duffymo's approach of building DAO classes, although it is better suited for a situation where the SQL queries are different.