OCP Question 26, Explanation

Which action can be used to load a database driver by using JDBC3.0?

A. Add the driver class to the META-INF/services folder of the JAR file.
B. Include the JDBC driver class in a jdbc.properties file.
C. Use the java.lang.Class.forName method to load the driver class.
D. Use the DriverManager.getDriver method to load the driver class.

 

The correct answer is C.

Every driver class must implement the interface java.sql.Driver. When a class implementing this interface is loaded into memory, it must create an instance of itself and register itself with the DriverManager class. Loading and registering a driver is done by calling method forName() from java.lang.Class, like this:

Class.forName("com.mysql.jdbc.Driver");

Manual loading of the drivers was required up to and including JDBC version 3.0, which means that the correct option is C.

Starting with JDBC 4.0, Java applications can automatically load and register the drivers using the Service Provider Mechanism (SPM), introduced in Java 6 and JDBC 4.0. An SPM-compatible driver needs to place the configuration file META-INF/services/java.sql.Driver into the .jar file (option A).

Specifying the JDBC driver in a properties files (option B) allows for customization, and the DriverManager.getDriver(String url) method in option D makes DriverManager to attempt to select a driver that understand the given url from the set of registered JDBC drivers.

Leave Comment

Your email address will not be published. Required fields are marked *