nilamber
nilamber

Reputation: 371

Why we call Class.forname("com.mysql.jdbc.Driver") even if mysql.jar is added in class path

Why we call Class.forname("com.mysql.jdbc.Driver") even if mysql.jar is added in class path .I think System ClassLoader should load this class as well if not then can any body describe how definition of Driver is written such that it is not loaded when classes in the class path are loaded?

Upvotes: 1

Views: 3037

Answers (5)

nilamber
nilamber

Reputation: 371

See definition of Driver class where it register Driver and instatiate Driver class in a static block which is called when class is loaded into JVM

public class Driver extends NonRegisteringDriver implements java.sql.Driver {
    static {
        try {
            java.sql.DriverManager.registerDriver(new Driver());
        } catch (SQLException E) {
            throw new RuntimeException("Can't register driver!");
        }
    }
public Driver() throws SQLException {
        // Required for Class.forName().newInstance()
    }
}

If we write

    Driver dr=new Driver();
        Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/massiverun","root","admin");
        System.out.println("Print"+con);

then class Driver will be instantiated twice and also static block will be called when new Driver() is called .if we Use Class.forname("com.mysql.jdbc.Driver") in place of new Driver() Driver class is instantiated once and if class is already loaded into JVM static block is not called for second time.

Upvotes: 1

Ashish
Ashish

Reputation: 1941

old Java versions for DriverManager to know about available JDBC drivers no this is not needed.

A call to Class.forName("X") causes the class named X to be dynamically loaded (at runtime). A call to forName("X") causes the class named X to be initialized (i.e., JVM executes all its static block after class loading). Class.forName("X") returns the Class object associated with the "X" class. The returned Class object is not an instance of the "x" class itself.

Class.forName("X") loads the class if it not already loaded. The JVM keeps track of all the classes that have been previously loaded. This method uses the classloader of the class that invokes it. The "X" is the fully qualified name of the desired class.

JDBC Driver Is a Good Example

You may have experience working with JDBC Drivers. For example, the classloader attempts to load and link the Driver class in the "org.gjt.mm.mysql" package. If successful, the static initializer is called.

Class.forName("org.gjt.mm.mysql.Driver");
Connection con = DriverManager.getConnection(url,?myLogin", "myPassword");

Upvotes: 1

Aniket Thakur
Aniket Thakur

Reputation: 68915

No need to call Class.forname("com.mysql.jdbc.Driver") from Java 7 which has JDBC 4.

Upvotes: 2

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 135992

It was used in old Java versions for DriverManager to know about available JDBC drivers. No need to use it now, especially when you explicitly give driver class name.

Upvotes: 2

Neeraj Krishna
Neeraj Krishna

Reputation: 1615

You can read about this here: What is the actual use of Class.forName("oracle.jdbc.driver.OracleDriver") while connecting to a database? .. the driver could be different.. the concept remains the same.

Upvotes: 1

Related Questions