It's not a secret anymore!
The moose likes JDBC and Relational Databases and the fly likes The Class class 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 "The Class class" Watch "The Class class" New topic

The Class class

Nicholas Carrier
Ranch Hand

Joined: Apr 14, 2005
Posts: 78
This could possibly go into the beginner section, but since I'm just learning JDBC I figure I'll post it here.

There's a line of code that says


Now, what that looked like to me was a Static Method being called from the Class class.

So I went to the API for Class and found that Class.forName()

Returns the Class object associated with the class or interface with the given string name.

So that means that it returns the class object associated with the given String name, which in this case is the String driver, and then calls newIntance(); on the Class object that was returned from the forName(method).

The problem is, is that I have absolutely no idea what this is for (and the API didn't really explain it), and at no point in the code (attached link) does it seem to do anything.

Would someone be kind enough to explain it me or point me in the right direction towards something that will?

I apologize ahead of time if it needs to be move, and thanks ahead of time for all of those who help.


source code
[ February 03, 2006: Message edited by: Nicholas Carrier ]

Teaching yourself anything is always the cheapest way, but it definitely takes a lot of time and effort.<br /> <br />Thank you javaranch <a href="http://""" target="_blank" rel="nofollow">Learn How to Ask Your Question</a> and be nice
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33130

What that's doing is loading the driver class into memory. That way when you try to get a connection from the driver, the JVM knows about it.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Nicholas Carrier
Ranch Hand

Joined: Apr 14, 2005
Posts: 78
Aight (sic), I think get it.

stu derby
Ranch Hand

Joined: Dec 15, 2005
Posts: 333
A proper JDBC driver always has a static initialization section, invoked when the class is loaded, that causes the driver to call DriverManager.registerDriver().

Then later, when DriverManager.getConnection() is called, it will iterate through all the loaded drivers, asking them (Driver).acceptsURL() ?, until it finds one that does, or exhausts the lists of drivers.
I agree. Here's the link:
subject: The Class class
It's not a secret anymore!