Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class.forName(JDBC_DRIVER) no longer needed?

Tags:

java

sql

jdbc

I've read here on SO that since java 6 you no longer need to register JDBC Driver using:

Class.forName(JDBC_DRIVER);

because DriverManager uses the path located in system property "jdbc.drivers" to retrieve the correct driver.

But when I do the followng:

System.out.print(System.getProperty("jdbc.drivers"));

null gets printed.

Do you have any clue why my app works correctly ?? ;)

like image 864
GionJh Avatar asked Jan 29 '15 17:01

GionJh


People also ask

Does JDBC require class forName?

forName is no longer required. Just put any JDBC 4. x driver in the project classpath, and Java is able to detect it.

What does class forName do in JDBC?

Use the Class. forName() method to load the driver. The forName() method dynamically loads a Java class at runtime. When an application calls the forName() method, the Java Virtual Machine (JVM) attempts to find the compiled form (the bytecode) that implements the requested class.

Why do we use class forName com mysql JDBC driver?

The Class class is located in the java. lang package, so it is distributed with java, and imported automatically into every class. What the forName() method does, is just return the Class object for the paramater that was loaded by the class loader. The newInstance() method then returns a new instance of the class.

How do I know if JDBC driver is installed Windows?

You can determine the version of the JDBC driver that you installed, by calling the getDriverVersion method of the OracleDatabaseMetaData class. You can also determine the version of the JDBC driver by executing the following commands: java -jar ojdbc5. jar.


2 Answers

That has nothing to do with that system property. Java6 (and JDBC4) introduced a concept known as "service provider" where implementations of known interface can be detected by the JVM during startup. A driver that is compliant with that will be registered by the DriverManager automatically. That's why Class.forName() is no longer necessary - but only if the driver supports that.

The service registration is initiated if there is a services directory in the driver's jar file inside the META-INF directory. That directory needs to contain a text file with the name of the interface that is implemented in the case of a JDBC driver that is java.sql.Driver containing the implementing class.

like image 192
a_horse_with_no_name Avatar answered Oct 30 '22 10:10

a_horse_with_no_name


From the Javadocs of DriverManager:

As part of its initialization, the DriverManager class will attempt to load the driver classes referenced in the "jdbc.drivers" system property. This allows a user to customize the JDBC Drivers used by their applications. For example in your ~/.hotjava/properties file you might specify:

jdbc.drivers=foo.bah.Driver:wombat.sql.Driver:bad.taste.ourDriver

This means that the system property need not be specified (as it says DriverManager will attempt). There is another mechanism through which drivers are automatically loaded, which relies on service loading since Java 6:

The DriverManager methods getConnection and getDrivers have been enhanced to support the Java Standard Edition Service Provider mechanism. JDBC 4.0 Drivers must include the file META-INF/services/java.sql.Driver. This file contains the name of the JDBC drivers implementation of java.sql.Driver.

Almost all JDBC drivers now conform to this requirement. Note that DriverManager does not internally fill the jdbc.drivers property, so it's still null.

like image 40
M A Avatar answered Oct 30 '22 09:10

M A