Tutorial :In Java, connecting to mysql, what is the meaning of Class.forName?



Question:

What is the purpose of this line?
It does not return a value or set the state of an existing class/object (or is it?)

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


Solution:1

It uses reflection to look on the classpath for a class called "com.mysql.jdbc.Driver" and makes a new instance of it.

In your code when you write

Integer foo = new Integer()  

You could instead write

Integer foo = Class.forName("java.lang.Integer").newInstance()  

But why go to all this trouble? Because you want to load your database driver at runtime, not hard code it in. So if you change databases, you just changes a config file that would load a different database driver. In your specific case it may not matter, but it does open up new possibilities on database configuration (and this Class.forName jazz is how it is usually done)


Solution:2

Almost certainly, com.mysql.jdbc.Driver has a static initializer that looks like this:

static {java.sql.DriverManager.registerDriver(new com.mysql.jdbc.Driver())};  

This static initializer is called when you use the forName method. So without realizing it you registered the MySQL driver.

As for the newInstance, I don't know why it is there. It seems unnecessary.


Solution:3

It loads the MySQL JDBC driver.

The reason you do this is you don't directly use this class so there is no static binding to the class name. When you attempt to open a database connection you simply use a java.sql.Connection and not a MySQL specific class or interface. That translation is done behind the scenes with dynamic class loading.

You're loading that class so it can register itself with the JDBC subsystem for when you make the connection subsequently.


Solution:4

Its purpose it's to dynamically load the class definition specified by the fully qualified name passed as string argument.

This means that the JVM will effectively search for the .class file inside classpath items and then with newInstance() you ask for a new instance of that class.

If the class had already been loaded before then it's already in cache and will not be retrieved from the file again. (not 100% sure about it, maybe you are allowed to "update" a class definition at runtime but personally I think it would create inconsistencies)


Solution:5

Calling Class.forName creates an instance of your driver.


Solution:6

A call to Class.forName("X") causes the class named X to be dynamically loaded (at runtime).


Solution:7

When you run java program, you have classpath and default location to pick up some jars (contain classes) and classes. The JVM will load all those jars (contain classes) and classes to prepare starting your java program.

Class.forName("className") look into all this loaded classes to return the particular "className" for you to create an instance/object from it.

It is a dynamic way of doing the same as in compile time object instantiation:

ClassName classInstance = new ClassName();  


Solution:8

Class.forName is used to load the Class object for the specified full class name. When the object, here refers to com.mysql.jdbc.Driver, is loaded, the static expression will be called. So although Class.newInstance() is not invoked to create a new instance, there must be some static initialization in com.mysql.jdbc.Driver.


Solution:9

Class.forName(String name)

Returns the Class object associated with the class [...] the given string name

That means, that, it would return ( or attempt ) to return the class com.mysql.jdbc.Driver

Later Class.newInstace()

Creates a new instance of the class represented by this [...] object

So, what that line does, is to create dynamically a mysql Driver instance.

This:

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

Would be equivalent to:

import java.sql.Driver;  ....   Driver driver = new com.mysql.jdbc.Driver()  

By the way according to DriverManager doc, you don't have to use it anymore:

Applications no longer need to explictly load JDBC drivers using Class.forName()

It use to be needed to load the driver, but now the DriverManager uses other strategy.

Finally, read the:

NOTE: The DataSource interface, new in the JDBC 2.0 API, provides another way to connect to a data source. The use of a DataSource object is the preferred means of connecting to a data source.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »