aspose file tools*
The moose likes JDBC and the fly likes Use of Class.forName() in JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Use of Class.forName() in JDBC " Watch "Use of Class.forName() in JDBC " New topic
Author

Use of Class.forName() in JDBC

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9519
    
    2

Guys,

What exactly does Class.forName() does....I know that it loads the driver into the JVM but later, how does that driver gets registered with the DriverManager class??

Also, can anyone explain me the difference between ClassLoader.loadClass() and Class.forName()??

Thansk in advance!


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
how does that driver gets registered with the DriverManager class??

The javadocs for java.sql.Driver talk about that. It involves calling DriverManager.registerDriver.

can anyone explain me the difference between ClassLoader.loadClass() and Class.forName()??

Have you tried using ClassLoader.loadClass instead of Class.forName to load a JDBC driver? If not, do so; if there is a difference, it should become apparent then.

BTW, one question mark per question is sufficient.
[ June 26, 2007: Message edited by: Ulf Dittmer ]

Ping & DNS - my free Android networking tools app
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9519
    
    2

BTW, one question mark per question is sufficient.


Agreed

I never tried using ClassLoader.loadClass instead of Class.forName...does the difference lies between the execution time for loading the classes?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Originally posted by Jothi Shankar Kumar Sankararaj:


Agreed

I never tried using ClassLoader.loadClass instead of Class.forName...does the difference lies between the execution time for loading the classes?


No. Try it, you'll see the difference.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
How can this be possible,

ClassLoader.loadClass ; loadClass is not a static method , its a non static method of class ClassLoader.

Regarding : how does that driver gets registered with the DriverManager class??

Try decompiling any driver class , you will find a static block doing the registration stuff.
[ June 26, 2007: Message edited by: Rahul Bhattacharjee ]

Rahul Bhattacharjee
LinkedIn - Blog
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I might guess that "static block" is the truly new information here. If you put code in a block like this:

then the JVM runs that code when ... um, you look it up. Anyhow, if it looks magical that you just think about a class with Class.forName() and it gets registered, there's the magic.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
static block would get executed when the class gets loaded (creation of instance is not required), so loading a class might be enough for registering a driver.

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9519
    
    2

By the way how can I decompile a class?
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Jothi Shankar Kumar Sankararaj:
By the way how can I decompile a class?


Using a java decompiler.
You might want to try DJ.
http://members.fortunecity.com/neshkov/dj.html

Or see the source of the Driver class.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
Originally posted by Jothi Shankar Kumar Sankararaj:
By the way how can I decompile a class?


Keep in mind that this is often illegal. E.g., the license for the JDK forbids this. Of course, the JDK class library comes with its own source in a file called "src.zip", so this isn't even necessary.

If it's third-party drivers, then there, too, it shouldn't be necessary. Either they're open source (in which case it's easier to just look at the source), or they're commercial, in which case the license forbids decompilation.
[ June 26, 2007: Message edited by: Ulf Dittmer ]
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
Originally posted by Ulf Dittmer:
or they're commercial, in which case the license forbids decompilation.
[/QB]


Thanks for this information.I was unaware that some license might forbid decompilation.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9519
    
    2

Thanks for the information on decompiling a class and the rules that are applicable for that!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Use of Class.forName() in JDBC