• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

On class.forname() in JDBC

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have below questions on class.forname() statement in JDBC connecion

1. If we write the code like class.forname("com.a.b.c");class.forname("com.p.q.r");...
then will JVM load all classes or it will load only first one (or last one) and will ignore all others?

2. Also after using class.forname(("com.a.b.c"); we get connection as DriverManager.getConnection("jdbc:mysql:///test",user,pass) so then how exactly database url finds its corrosponding class which has been loaded using class.forname(). (if it uses reflextion, then can anybody pls tell me how it exactly works).

Regards
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
#1) The JVM will load all classes. Why would it ignore code statements?

#2) During class initialization (which happens when Class.forName is called), the driver register itself with JDBC, specifically stating for which JDBC URLs it can be used. From the javadocs of DriverManager:

When the method getConnection is called, the DriverManager will attempt to locate a suitable driver from amongst those loaded at initialization and those loaded explicitly using the same classloader as the current applet or application.

 
reply
    Bookmark Topic Watch Topic
  • New Topic