This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


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

Class.forName() ?

AmitVijay AVKulkarni
Greenhorn

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

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

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
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
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
Greenhorn

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
Greenhorn

Joined: Mar 05, 2005
Posts: 16
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

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.

Dave
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class.forName() ?
 
Similar Threads
No suitable driver found for jdbc:oracle:thin:@localhost:1521:Indu
Class.forName in depth
Connecting to remote database
Registering the drivers
What is this No Suitable Driver exception?