The paragraph 9.2 of the JDBC4 specification, states that the Driver
implementation must register itself to the DriverManager
upon class loading, so that when the Driver
implementation is loaded the static initializer will automatically
register an instance of the driver.
So, simply loading the Driver
implementation by (Class.forName("driverClassName")
), will register the driver with the DriverManager
.
Alternatively, the specification provides a mean to externally specify the drivers to be loaded (and thus registered) by the DriverManager
, through the system property jdbc.drivers
(see paragraph 9.2.1):
java -Djdbc.drivers=com.acme.jdbc.AcmeJdbcDriver Test
These methods of registration are available also in old JDBC3 implementations.
JDBC4 introdces a new method of registration, leveraging the Service Provider Mechanism:
every compliant driver must provide a jar including the META-INF/services/java.sql.Driver
file.
The DriverManager
(on DriverManager.getConnection()
calls ) uses the java.sql.Driver
service provider and loads the classes there specified, thus automatically registering the driver. This removes the need to invoke Class.forName
(see paragraph 9.2.1 and paragraph 3.1, first bullet).