Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better understaning - Class.forName("com.mysql.jdbc.Driver").newInstance ();

Tags:

java

mysql

I came across this helpful link with code which works perfectly when updated to hit against my web server. I can do absolutely everything.

Now, the only thing I do not fully understand is the Class.forName().

Why is this being used? Can this be done differently? Is this a work around for something else? Adding a reference? Creating a class as implementing/extending another one?

I want to fully understand what is going on, but this is in my way.

Thank you

like image 690
jason m Avatar asked Oct 17 '12 11:10

jason m


People also ask

What does class forName com MySQL JDBC driver do?

It create a new instance of the com. mysql. jdbc. Driver class and Register the driver.

Why do we write class forName () in JDBC?

Use the Class. forName() method to load the driver. The forName() method dynamically loads a Java class at runtime. When an application calls the forName() method, the Java Virtual Machine (JVM) attempts to find the compiled form (the bytecode) that implements the requested class.

What is class forName used for?

The Class. forName is used to load any given class (within double quotes as String) at run time. For example, when we use IDE, we see there will be a GUI builder which allows us to drag and drop the buttons, text fields, etc. This drag and drop mechanism internally requires certain classes to be loaded at run time.


3 Answers

That code is forcing the class representing the MySQL driver to load and initialize. In Java, a class is not loaded unless it is necessary that the class gets loaded. Since JDBC code usually never directly references the driver, it wouldn't get loaded without Class.forName (or some other equivalent alternatives).

Note that it is necessary to both load and initialize the class, which are 2 different things.

Also, note that it is not necessary to call .newInstance() -- the static initializer of the Driver already registers itself as a JDBC driver.

Finally, note that with the Service Loader API it is usually not necessary to call Class.forName() to load the driver: it can be loaded automatically.

like image 88
Bruno Reis Avatar answered Oct 21 '22 05:10

Bruno Reis


Class.forName(className) loads the class with the specified className.

JDBC drivers are loaded this way to avoid having to have a compile-time dependency on a specific JDBC driver. The idea is that you use Java's JDBC API (the classes and interfaces defined in the packages java.sql and javax.sql) without having to refer directly to a particular JDBC driver.

When you let Java load the driver class with the forName call, the driver will register itself so that it can be used.

Note that for most JDBC drivers it's unnecessary to explicitly create a new instance of the driver class; you can leave off the .newInstance() call.

Note that since JDBC version 4.0, the Class.forName() call isn't necessary anymore - the process to discover drivers has been improved, JDBC can load them automatically.

like image 26
Jesper Avatar answered Oct 21 '22 04:10

Jesper


All JDBC Drivers have a static block that registers itself with DriverManager and DriverManager has static an initializer only.

The MySQL JDBC Driver has a static initializer looks like this:

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

JVM executes the static block and the Driver registers itself with the DriverManager. You need a database connection to manipulate the database. In order to create the connection to the database, the DriverManager class has to know which database driver you want to use. It does that by iterating over the array (internally a Vector) of drivers that have registered with it and calls the acceptsURL(url) method on each driver in the array, effectively asking the driver to tell it whether or not it can handle the JDBC URL.

like image 5
Ta Duy Anh Avatar answered Oct 21 '22 03:10

Ta Duy Anh