• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Use of Class.forName() in JDBC

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the information on decompiling a class and the rules that are applicable for that!
 
He's my best friend. Not yours. Mine. You can have this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic