• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class.forName() ?

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Dear all,

I am using JDBC Type 3 driver for Oracle database. I am using classes12.jar. In this we have a class called oracle.jdbc.driver.OracleDriver. When we are creating a connection with a database first statement we write is

Class.forName("oracle.jdbc.driver.OracleDriver");

Can anybody tell me exactly how this functions? I mean when we say Class.forName("ABC"), this causes class named ABC to be initilized or it creates a copy of the class ABC.

What happens to copy of class ABC if it is already there?

~ Amit
[ July 27, 2005: Message edited by: Bear Bibeault ]
 
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
You'll find better answers if you search the forum, but the short answer is that 'Class.forName() causes the class to be loaded, and when Drivers are loaded they register themselves with the DriverManager. If you call it multiple times the class will only load itself once and register iteself once.
 
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Okay here is the answer.
When u say Class.forName("ABC");
Class.forName gets executed. it loads the class "ABC".
In class ABC, there is a static block which gets executed as stated below

public class ABC
{
ABC abc = null ;

static
{
if(abc == null)
{
//loads and creates a new object if it does
not exist
ABC abc = new ABC();
//then it registers with the Driver Manager using
DriverManager.registerDriver(abc);
// this is a static method check
the DriverManager API
}
}
}

Hope the above code resolves ur issue. Let me know if u want any more help or check out for JDBC specification pdf on sun site.

Regards
Makarand Parab
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean by "class loading?"
where are the classes loaded when its passed?
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the JDBC Driver Contract.

Class Loading is a complex mechanism, which I advice you not to deal with for the time being.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why can't we use just create a new object with that class ??

Like

driver = New JDBCDDriver();

I mean we put the Initializer Code in the constructor.

Sorry for my bad English. *sigh*

 
David O'Meara
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
If you create your own Driver, your code becomes 'coupled' with that Driver. You won't be able to use a different database without recompiling your code.

If you use Class.forName(string) and DriverManager.getConnection(string), then you can pass the string values in at run time and change it each time the application is run, or even distribute your application and allow users to choose their own dtabaase rather than being forced to use just one.

Dave
reply
    Bookmark Topic Watch Topic
  • New Topic