• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Regaring class.forname in java

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

why we need to load the jdbc driver class using class.forname( oracle.jdbc.driver.oracledriver ), Already all the classes is loaded by class loader right?

Could you please give me some information regarding class.forname.

Thanks,
Santhosh Kumar V.K
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Already all the classes is loaded by class loader right?


Wrong. The classloader only loads classes when (and if) they're needed.
 
Rancher
Posts: 1043
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class.forName loads the named class dynamically: it is not necessary to know the name at compile time, it can be configured at runtime.

If you program against standard SQL, that is, in a portable way, then the same Java code can run against several different RDBM systems (Oracle, MySQL, MS SQL, Postgres, Sybase, whatnot) without recompilation, if the driver name (and of course the url and the login information) are provided at run-time.

It is not necessary to use Class.forName though: instead of

you can simply do anything else to enforce the said class getting loaded

Then you will have a compile-time dependency on the driver.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that in newer JDBC versions, it is not necessary anymore to explicitly load the JDBC driver with Class.forName(...), because there's a new mechanism so that JDBC can automatically find and load drivers for you.

When you use an older JDBC driver, you need to do this explicitly so that the class is loaded and initialized.

Ivan Jozsef Balazs wrote:you can simply do anything else to enforce the said class getting loaded

Then you will have a compile-time dependency on the driver.


That's not a good idea, however.
 
reply
    Bookmark Topic Watch Topic
  • New Topic