1

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?

Ashish
  • 1,943
  • 2
  • 14
  • 17
nilamber
  • 371
  • 2
  • 10
  • Originally there was no magic mechanism that made drivers available to the DriverManager. Now there is, but online examples tend to live very long and it doesn't hurt. – Thorbjørn Ravn Andersen Jan 18 '14 at 10:23
  • possible duplicate of [What is the actual use of Class.forName("oracle.jdbc.driver.OracleDriver") while connecting to a DataBase?](http://stackoverflow.com/questions/8053095/what-is-the-actual-use-of-class-fornameoracle-jdbc-driver-oracledriver-while) – Mark Rotteveel Jan 18 '14 at 11:38

5 Answers5

2

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.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
2

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

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • 1
    AFAIK it's since Java SE 6. – Puce Jan 18 '14 at 10:19
  • http://docs.oracle.com/javase/7/docs/api/java/sql/package-summary.html#package_description – Puce Jan 18 '14 at 10:20
  • Maybe introduced in later versions of Java 6? Docs still say Java 6 uses JDBC 3 API. http://docs.oracle.com/javase/6/docs/technotes/guides/jdbc/ – Aniket Thakur Jan 18 '14 at 10:30
  • 1
    Java 6 was JDBC 4.0, which introduced automatic driver loading, it simply looks like that page wasn't updated. – Mark Rotteveel Jan 18 '14 at 11:36
  • @AniketThakur Also see the Javadoc link I've posted where you can see the JDBC versions and what changed. Java SE 7 comes with JDBC 4.1 – Puce Jan 18 '14 at 12:40
  • AFAIK automatic driver loading is based on java.util.ServiceLoader which also has been introduced in Java SE 6. – Puce Jan 18 '14 at 12:55
1

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.

Community
  • 1
  • 1
Neeraj Krishna
  • 1,527
  • 2
  • 16
  • 22
1

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");
Ashish
  • 1,943
  • 2
  • 14
  • 17
1

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.

Sankumarsingh
  • 9,889
  • 11
  • 50
  • 74
nilamber
  • 371
  • 2
  • 10