jQuery in Action, 2nd edition*
The moose likes JDBC and the fly likes Class.forName Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Class.forName" Watch "Class.forName" New topic
Author

Class.forName

shar ball
Greenhorn

Joined: Jul 14, 2005
Posts: 6
It is said that Class.forName(String driver), loads the driver and register the driver with the Driver Manager. But how does Class.forName() register the driver with the Driver Manager? It is a simple method from the "Class" class.
Can anyone let me know about this?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Class.forName(String) does not register the class with the DriverManger. The DriverManager looks for a loaded class which can support the JDBC url it has been asked to get a connection with. Class.forName(String) loads and initializes the class of that name, once that is done the DriverManager can find it.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

more specifically when clsss.forName() loads the driver before initialisation, driver is supposed to register itself with DriverManager using its static initializer.

As per JDBC guide by Sun


All Driver classes should be written with a static section (a static initializer) that creates an instance of the class and then registers it with the DriverManager class when it is loaded. Thus, a user would not normally call DriverManager.registerDriver directly; it should be called automatically by a Driver class when it is loaded



Shailesh


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
sinasi susam
Ranch Hand

Joined: Jul 15, 2005
Posts: 67
why dont we import all namespaces of Driver and DriverManager classes?

Why; Class.forName("...");

what does it do?
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
When you use a class directly, the JVM will load the class and initialize it automatically. For exaple, when you do something like

The String class is loaded and initialized. This means that memory for static variables is allocated and their values are initialized. Static initialzers are also executed. Similarly when you do

The Connection and DriverManager classes are loaded (if they aren't already loaded, that is) before the getConnection() method is even called.

However, we never use JDBC driver classes directly, so how are they loaded? By calling Class.forName()! This method will also load a class if it isn't already loaded. When the driver class is loaded, the static initializers in the class run. This is when the driver is registered with the DriverManager, as described above.

If you are interested in learning more, I suggest you look at the javadocs for Class, specifically the forName() method. You should also look at ClassLoader.

Layne


Java API Documentation
The Java Tutorial
sinasi susam
Ranch Hand

Joined: Jul 15, 2005
Posts: 67
The String class is loaded and initialized. This means that memory for static variables is allocated and their values are initialized. Static initialzers are also executed.


what do you mean by that?

That was not a static variable?Am i right?




I think if its something like this, its initialized.Because we dont need to get new insatance of class A.We will reach it by A.str.However, i dont know when they are initilized?
[ July 16, 2005: Message edited by: sinasi susam ]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Nope, a static initialiser is a special block of code that will only be executed once when the Class is loaded. It is possible to load a Class several times in a single JVM and even reload a Class, but for simplicity sake assume that the block will only be run once when the class is first loaded.

It looks like this:



I haven't compiled this, but it should work. Class.forName() causes the static block to 'fire' the first time, the static block creates an instance and registers it with the DriverManager. Subsequent calls to Class.forName() don't do anything.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Class.forName