Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of Class.forName() in JDBC

 
Joe Harry
Ranch Hand
Posts: 10123
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Joe Harry
Ranch Hand
Posts: 10123
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10123
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way how can I decompile a class?
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10123
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the information on decompiling a class and the rules that are applicable for that!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic