• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

DriverManager lookup

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

Why should we load the DriverManager explicitly in the JDBC code using Class.forName()?
Why can't it be included in the classpath and let the classloader load it? The approach could even have been like have the jar file like ojdbc14.jar in the classpath.
Why is the apporach to the loading of DriverManager so different from how we otherwise instantiate the other classes?

Thanks,
Vidya
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be clear: you don't load the DriverManager (a class in the JDBC package), but rather, you load the JDBC driver for a given database.

The reason you do this is that the java.sql package is a set of interfaces, and you write all your code to those interfaces. A driver is a set of classes that implement those interfaces and let you talk to a specific database. Often, the classes in that driver aren't even public classes, so you can't use their names directly. Instead, you can load the driver, which registers itself with the DriverManager, which allows your application to get a Connection to that particular brand of database. In theory, if you change just the String containing the name of the driver class, you can load a different driver, connect to a different brand of database, and all of the rest of your code can remain exactly the same. Now, in most real applications, the name of the driver class is read from a configuration file, not hardcoded into the application. This means that by editing a text file, you can configure your applciation to use Oracle, or MySQL, or MS-SQL, without changing the code at all!
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Neelima Mohan wrote:Why is the apporach to the loading of DriverManager so different from how we otherwise instantiate the other classes?


It isn't, necessarily, depending on how your applications are encoded. Think of it as a form of dependency injection, except rather than injecting the actual implementation, you're "injecting" the string name of the class to use.
 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to JDBC.
 
Nothing up my sleeve ... and ... presto! A tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic