aspose file tools
The moose likes JDBC and Relational Databases 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 and Relational Databases
Bookmark "Class.forName() ?" Watch "Class.forName() ?" New topic

Class.forName() ?

AmitVijay AVKulkarni

Joined: Apr 26, 2005
Posts: 17

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


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 ]
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

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.
Makarand Parab
Ranch Hand

Joined: Dec 10, 2004
Posts: 121
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 ;

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
// 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.

Makarand Parab
sinasi susam
Ranch Hand

Joined: Jul 15, 2005
Posts: 67
What do you mean by "class loading?"
where are the classes loaded when its passed?
Yilmaz Mete

Joined: Dec 23, 2003
Posts: 28
Here is the JDBC Driver Contract.

Class Loading is a complex mechanism, which I advice you not to deal with for the time being.
Alex Prawira

Joined: Mar 05, 2005
Posts: 16
Why can't we use just create a new object with that class ??


driver = New JDBCDDriver();

I mean we put the Initializer Code in the constructor.

Sorry for my bad English. *sigh*

David O'Meara

Joined: Mar 06, 2001
Posts: 13459

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.

subject: Class.forName() ?