Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is driver class located in JDBC4

One of the great additions in version 4 of JDBC You don't have to explicitly load the driver by calling Class.forName anymore. When your application attempts to connect the database for the first time, DriverManager automatically loads the driver found in the application CLASSPATH.

My question is how? What if there are multiple drivers in the classpath?

One thing I can guess is that on parsing the connection URL whether driver needed is of JDBC or ODBC can be figured out but how can one say out of multiple jdbc compliant drivers which one is to be selected for the database I am using? (lets say I am using MySql and I need MySql-Connector driver). Is there any static mapping of such database drivers in JVM?

like image 893
Aniket Thakur Avatar asked Aug 17 '13 11:08

Aniket Thakur


People also ask

Where is the JDBC driver located?

The JDBC driver files are installed in C:\program files\microsoft SQL server <ver> JDBC Driver\lib.

Where is MySQL JDBC driver located?

Installing the JDBC Driver for MySQL Databases Locate the mysql-connector-java-<version>-bin. jar file among the files that were installed. For example, on Windows: C:\Program Files (x86)\MySQL\MySQL Connector J\mysql-connector-java-5.1. 30-bin.

What is the driver manager class?

The DriverManager provides a basic service for managing a set of JDBC drivers. 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.

What is the use of driver class?

Driver classes are the utility classes that are used to carry out some task. In Java, driver classes are used in JDBC to connect a Java application to a database. Driver classes are vendor-specific i. e. MySQL database provides its own driver class, and Oracle database provides its own class as well.


1 Answers

Every JDBC 4 compliant driver has a file in its jar called META-INF/services/java.sql.Driver, in that file it will list its implementation(s) of java.sql.Driver. When you request a connection, DriverManager will use the ServiceLoader to find all(!) copies of META-INF/services/java.sql.Driver in the classpath and will then load all classes listed. When a java.sql.Driver class is loaded, it has to register itself with the DriverManager, so the DriverManager loads all classes using the service loader, and each Driver implementation registers itself.

When you request a connection from DriverManager, the DriverManager will iterate over all registered drivers asking them for a Connection. The driver will use the JDBC url to check if it's a protocol it supports (eg Jaybird/Firebird JDBC checks if the url starts with "jdbc:firebirdsql:" or "jdbc:firebird:"). If the driver does not support the protocol, it will return null, if it does support the protocol it will either return an established connection, or it will throw an SQLException (eg if you made an error in the URL, or it couldn't connect). If all drivers return null (none support the protocol), then DriverManager will throw an SQLException with error "No suitable driver found for <url>"

So, having multiple drivers on the classpath does not matter as long as they support different protocols, however if there are multiple drivers for the same database (or at least: same protocol prefixes), it will use the first in the list of drivers. Depending on the Java version, if that driver fails with an SQLException, it will continue with the next driver (at least Java 5 and later), or stop trying and throw the exception (I believe this was in Java 1.4 or maybe even earlier).

like image 198
Mark Rotteveel Avatar answered Oct 23 '22 18:10

Mark Rotteveel