wood burning stoves 2.0*
The moose likes JDBC and the fly likes Load the class thru class.forName() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Load the class thru class.forName() method" Watch "Load the class thru class.forName() method" New topic

Load the class thru class.forName() method

Suresh Gangapatnam

Joined: Apr 12, 2012
Posts: 1
What happens internally while loading the class thru class.forName("Driver")?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38045
Welcome to the Ranch

I presume by Driver you mean something like com.databasecompany.drivers.Driver. If you look up the static Class#forName(String) method, you find it returns the Class<?> object of that name. It is a little like a static counterpart to the instance method Object#getClass(). Now, to get a Class<?> object, you have to have the Class in JVM memory, so if the class of that name has not yet been loaded, it loads it into JVM memory. And whenever you load a class into memory, its static members are loaded too, and any static initialisers are run (you can tell that from the list of Exceptions shown in the link.
Most JDBC driver classes have a static initialiser which sets up the connection to the database management program. Running Class.forName(...) at the start of the program will call that static initialiser and connect you to the database.
Ravikanth thota

Joined: Apr 09, 2012
Posts: 24
actually question should be Class.forName() [C-capital]

here Class is the name of the class belongs to java.lang package

forName() is the static method which loads the class from secondary memory to primary memory

here Class.forName() loads the driver class from secondary memory to primary memory.
I agree. Here's the link: http://aspose.com/file-tools
subject: Load the class thru class.forName() method
Similar Threads
How to connect MySQL DB from JSP?
JDBC, mysql connector/j, face a problem to connect mysql 5.1 and JSE.
JDBC driver
jdbc with microsoft SQl Server 2008