• Software
  • Leadership
  • Agile
  • Events
  • Other Topics
    • Finance
    • Robotics & AI
    • System Administration
    • Books
    • Life Experiences
    • Environment
  • Write and Earn
  • About Us
    • About Us
    • Our Contributors
    • Contact Us
    • Article Submission Guidelines
    • Logo demystified
  • Follow @MeJaneve
    Janeve.Me
  • Categories

    Browse through following categories for articles written by professionals.
    • Agile
      4
    • Books
      5
    • Environment
      1
    • Events and Conferences
      7
    • Featured
      9
    • Finance
      1
    • Leadership
      6
    • Life Experiences
      8
    • Robotics & AI
      1
    • Software
      30
    • System Administration
      2
  • Software
  • Leadership
  • Agile
  • Events
  • Other Topics
    • Finance
    • Robotics & AI
    • System Administration
    • Books
    • Life Experiences
    • Environment
  • Write and Earn
  • About Us
    • About Us
    • Our Contributors
    • Contact Us
    • Article Submission Guidelines
    • Logo demystified
Home » Software

Class.forName() and DriverManager.getConnection()

Janeve George Posted On February 28, 2012
5
12.9K Views


0
Shares
  • Share On Facebook
  • Tweet It

Have you ever wondered how the following code worked?

Class.forName("some.database.Driver");
Connection conn = DriverManager.getConnection("connection_url", "username", "password");

A half-backed answer would be Class.forName() loads the database driver class some.database.Driver, and DriverManager.getConnection() returns the appropriate Connection instance based on the ‘connection_url’ provided.

Let’s figure out what is actually happening.

The Class.forName() method is responsible of loading the class some.database.Driver. There is a static block within all Database Driver implementations which would look something like this:

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

The Driver class tries to register it’s own instance with the DriverManager.

Now when we call DriverManager.getConnection(), it iterates through all the drivers registered with it and checks the appropriate driver for the ‘connection_url’. If you examine the code in the class you would find a method getDriver():

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);
}

As you might have observed, the acceptsURL() method is called for each registered drivers and the first driver that believes it could connect to the specified database is returned.

Post Views: 12,876
0
Shares
  • Share On Facebook
  • Tweet It




Author

Janeve George

A Technology Leader, Software Engineer, and Agile Methodologies enthusiast. Currently, working as Lead Software Development with Zeta Suite. He has more than 1.8 decades of experience spanning different verticals predominated by hosting, cloud, and media delivery technologies.

Installing Haskell Platform
Read Next

Installing Haskell Platform

  • Follow @MeJaneve
    Janeve.Me
  • Categories

    Browse through following categories for articles written by professionals.
    • Agile
      4
    • Books
      5
    • Environment
      1
    • Events and Conferences
      7
    • Featured
      9
    • Finance
      1
    • Leadership
      6
    • Life Experiences
      8
    • Robotics & AI
      1
    • Software
      30
    • System Administration
      2

  • Popular Posts

  • Recent Posts

    • 3Es to Supercharge your Career Journey and Performance Review Ratings
    • Java Asynchronous Programming using CompletableFuture - Part 1
    • The Java Stream API
    • Functional Interfaces in Java
  • Keep In Touch

    Follow us on social media to get latest articles on Programming, System Architecture, Agile Development Methodologies, Product and Project Management, Personal Development, BigData, Robotics, Upcoming Events and more...


Copyright © 2020 | Janeve.Me. All rights Reserved.
Press enter/return to begin your search