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 .
-
From Java 7 you don't even have to to that. it will automatically be picked up from your class path. – Aniket Thakur Oct 30 '13 at 05:48
-
I used java 6. Please tell about the difference.. – user2815407 Oct 30 '13 at 05:51
-
3If I understand correctly, imports are handled at compile time while class.forName is handled at runtime. – W.K.S Oct 30 '13 at 05:52
-
could you explain briefly with example because I am in beginning stage of jdbc. – user2815407 Oct 30 '13 at 05:53
-
1may be this could help http://www.xyzws.com/Javafaq/what-does-classforname-method-do/17 – AJ. Oct 30 '13 at 05:59
7 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

- 6,527
- 8
- 50
- 85
-
When I tested my mysql retriving java program with a main class. at that time class.forname is not needed, I simply import the jdbc driver class by import statement. class.forname is needed only when call a jsp page in tomcat server. – user2815407 Oct 30 '13 at 06:05
-
can you please post your code from your core java program , how you created connection object? – Hussain Akhtar Wahid 'Ghouri' Oct 30 '13 at 06:07
-
see this [link](http://stackoverflow.com/questions/19638495/array-gives-null-as-output-in-the-browser) – user2815407 Oct 30 '13 at 06:09
-
I imported like import com.mysql.jdbc.Driver; It is worked when i tested witha main class java program. – user2815407 Oct 30 '13 at 06:47
-
Also, `Class.forName()` throws `ClassNotFoundException`. So you can use that to check whether the class exists, at runtime. Dunno if that is actually useful mind you! – jdurston Dec 31 '15 at 23:03
-
1Here is a well explained tutorial on youtube: https://www.youtube.com/watch?v=me9CcSXLHHc&list=PLsyeobzWxl7rU7Jz3zDRpqB-EODzBbHOI&index=5 – RAY Jun 05 '20 at 06:14
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.

- 1
- 1

- 66,731
- 38
- 279
- 289
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..

- 735
- 1
- 6
- 15
- import
When a class being used in its short name is about to loaded by JVM, the "import" phrase tells class loader where to find the class. So "import" doesn't load any classes, it just defines the location where classes are from. - Class.forName()
Load and initialize a class immediately into JVM.
BYW: 3 ways to load a class.
1) using "new" key word (load class + initialize class + new instance)
Dog d = new Dog();
2) using "Class.forName()" (load class + initialize class)
Class dogClass = Class.forName("Dog")
3) using "classLoader.loadClass()" (load class)

- 3,623
- 37
- 44
An "import" statement in Java allows you access to use a class or resource that you're important.
For example:
import my.package.Utils;
Now, I can access my Utils class in the current class I'm working in:
import my.package.Utils;
public static void main(String[] args) {
Utils.doStuff(); //I can do this because I have access to Utils in my import
}
Class.forName dynamically loads a class. This is useful in a situation where the class could change, such as a SQL driver implementation. JDBC doesn't know which class to import at compile time.
Calling Class.forName will not allow you to access it throughout the class you're currently working in, which a global import statement does.
With JDBC, we can use an abstracted class, such as java.sql.DataSource
, which defines a database. However, the MySQL DataSource and a Microsoft SQL DataSource are different implementations. JDBC doesn't know which one you want to use.
So you use DriverManager to specify to JDBC that you want to use the MySQL implementation, and you load that driver's class.
Say that later on you switch to Microsoft SQL. Now, all you have to do is change the DriverManager to load the Microsoft SQL driver's class, and all of your code that uses the abstract java.sql
classes will still work.

- 2,558
- 1
- 16
- 24
-
When I tested my mysql retriving java program with a main class. at that time class.forname is not needed, I simply import the jdbc driver class by import statement. class.forname is needed only when call a jsp page in tomcat server. – user2815407 Oct 30 '13 at 06:00
From docsClass#forName
public static Class<?> forName(String className)
throws ClassNotFoundException
Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to:
Class.forName(className, true, currentLoader)
where currentLoader
denotes the defining class loader of the current class.
For example, the following code fragment returns the runtime Class descriptor for the class named java.lang.Thread
:
Class t = Class.forName("java.lang.Thread")
A call to forName("X")
causes the class named X to be initialized.
From language specs jls-7.5
An import declaration makes types or members available by their simple names only within the compilation unit that actually contains the import declaration.
Another, less common form of import allows you to import the public nested classes of an enclosing class.

- 12,825
- 9
- 67
- 90
since you added jdbc tags i will answer in terms of that.
as Hussain Akhtar Wahid said Class.forName("name");
calls static initializers
java handles many SQL drivers via DriverManager, instead of writing code for each driver you only need to register that driver and DriverManger
will do the work for you.
static initializer in java.mysql.jdbc.Driver calss
static {
try {
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
what you do by calling Class.forName("com.mysql.jdbc.Driver");
is registering DriverManger to tell i want access mysql database.
snippet source

- 820
- 1
- 11
- 34