Both import and class.forName loads the class file. When I do an example of importing a mysql data in jsp file, It is needed to import the driver class through class.forName .when I import the jdbc driver through import statement it cant take data from mysql in tomcat server .
forName(String name, boolean initialize, ClassLoader loader) method returns the Class object associated with the class or interface with the given string name, using the given class loader. The specified class loader is used to load the class or interface.
Importing includes the knowledge of and functionality of a class or library in your current code. Inheriting includes the properties and functionality of a parent class in a child class.
14: When you say Class. forName() loads the driver class, does it mean it imports the driver class using import statement? No, it doesn't. An import statement tells the compiler which class to look for.
To import java package into a class, we need to use java import keyword which is used to access package and its classes into the java program. Use import to access built-in and user-defined packages into your java source file so that your class can refer to a class that is in another package by directly using its name.
1 : import
==> loads the class when you call any instance of it or call anything by class reference
==> loads the class when call is made
2 : Class.forName("");
==> loads the class in the jvm immediately
difference can be seen by if a class has static block
==> import will not call the static block
==> Class.forName("") will call the static block
in your case
===> Driver class when loaded by Class.forName("") , executes its static block , which published the driver
==> Simply importing the Driver class wont execute the static block and thus your Driver will not be published for connection objects to be created
A Driver class is loaded, and therefore automatically registered with the DriverManager by calling the method Class.forName. This explicitly loads the driver class. Since it does not depend on any external setup, this way of loading a driver is the recommended one for using the DriverManager framework. The following code loads the class acme.db.Driver:
Class.forName("acme.db.Driver");
If acme.db.Driver has been written so that loading it causes an instance to be created and also calls DriverManager.registerDriver with that instance as the parameter (as it should do), then it is in the DriverManager's list of drivers and available for creating a connection.
Picked up from this answer. Read it for detailed information. But I guess import will not register the driver under driver manager.
class.forName
allows you to use the Driver class without having an explicit import for your class. This allows you to build the project without having to have the jdbc driver in your classpath.
And the reason why " it cant take data from mysql ", as you possibly might not be returning reference to the variable regarding the jdbc driver class..
Hope it helps..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With