Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference Between import and class.forName in java

Tags:

java

jsp

jdbc

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 .

like image 1000
user2815407 Avatar asked Oct 30 '13 05:10

user2815407


People also ask

What is class forName Java?

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.

What is the difference between import and inheritance in Java?

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.

When you say class forName () loads the driver class does it mean it imports the driver class using import statement explain?

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.

What is a import class in Java?

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.


Video Answer


3 Answers

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
like image 198
Hussain Akhtar Wahid 'Ghouri' Avatar answered Sep 19 '22 10:09

Hussain Akhtar Wahid 'Ghouri'


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.

like image 23
Aniket Thakur Avatar answered Sep 19 '22 10:09

Aniket Thakur


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..

like image 31
Ashish Avatar answered Sep 21 '22 10:09

Ashish