• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Class.forName

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why dont we import all namespaces of Driver and DriverManager classes?

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

what does it do?
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
sinasi susam
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic