Loading Driver class before saying DriverManager.getConnection in JDBC call 4

These are some of the favorite lines we write for JDBC connectivity:

 Class.forName("com.mysql.jdbc.Driver"); 
 Connection connection = DriverManager.getConnection("url", "user", "password");

In this particular example, I have used the MySQL driver to connect to MySQL database.
This is also one of the favourite questions asked in interviews. First of all I can tell you what all questions can be asked in this topic:

  • What is this Class.forName statement doing here
  • Why is this loading of Driver class required for getting a connection from the database?
  • What if I didn’t write the Class.forName statement and directly call DriverManager.getConnection?
  • If I write multiple Class.forName statements before calling DriverManager.getConnection
    What will happen in that case?
  • java.sql.Connection is a class or an interface. (It is an interface). So next question would be if this is an interface where is the implementation of this interface?

Now we will try to figure out the answer of these questions one by one:
If we talk about the forName method of Class class, so it is a static method that loads the class passed as an argument in this method. This answers the first question.
As soon as the class gets loaded the static block written in that class executes, now we will see what is written in the static block of com.mysql.jdbc.Driver

static { 
  try{ java.sql.DriverManager.registerDriver( new Driver() ); 
   } catch (SQLException e) {
      throw new RuntimeException ("Can't register driver!") 
   } 
  }

It creates the instance of driver class and registers that driver with the DriverManager class using registerDriver method. So the loading of the Driver class is required to register the Driver object with the DriverManager. This answers the second question.
If we write multiple Class.forName statement before calling DriverManager.getConnection, then which Driver it will use.
For finding the answer, I have opened the source code of DriverManager class and checked the getDriver method.
In the DriverManager class, a collection named drivers is maintained to store all the registered drivers, so if you write multiple Class.forName statements with different Driver classes as argument, it will register all those driver classes with the DriverManager and all those classes will be added in the Collection named drivers.

public static Driver getDriver(String url) throws SQLException 
{ Enumeration e = drivers.elements(); 
  while(e.hasMoreElements()) { 
    Driver d = (Driver)e.nextElement(); 
    if ( d.acceptsURL(url) ) 
    return d; 
} throw new SQLException("No driver found for " + url); }

If you see the DriverManager.getDriver method above, you will see that we are iterating over the drivers collection and calling acceptURL method on each driver, this is the same URL we are giving in the getConnection method, So the Driver Class which is accepting this URL will be chosen as a driver to connect to the Database, if none of the drivers is able to accept it then SQLException will be thrown “No Driver found for” + URL. This answers my fourth question.
Also my third question is also answered, if you are not writing any Class.forName, so no Driver will get registered with the DriverManager and SQLException will be thrown “No Driver Found”.

 

 

4 thoughts on “Loading Driver class before saying DriverManager.getConnection in JDBC call

  1. Reply wellness blog Aug 24,2013 11:57 am

    Aw, this was a very nice post. Spending some time and actual effort to make a superb article… but what
    can I say… I put things off a whole lot and never manage to get
    anything done.

  2. Reply more Aug 29,2013 11:20 am

    Hey There. I found your blog using msn. This is an extremely well written article. I?ll make sure to bookmark it and return to read more of your useful info. Thanks for the post. I?ll certainly comeback.

  3. Reply Vishal Thakur Nov 26,2013 5:28 am

    Hi
    It was really good article. Thanks
    Can u provide please also answer the last question
    –> “java.sql.Connection is a class or an interface. (It is an interface). So next question would be if this is an interface where is the implementation of this interface?

  4. Reply Sunny Gupta Nov 26,2013 6:50 am

    Java.sql.Connection is an interface, its implementation is given by different database vendors, for example, for mysql we find its implementation in the mysql-connector-java.jar and in the similar way for hsqldb, its implementation is in hsqldb.jar, for Oracle you find it in classes12.jar

Leave a Reply