• 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

Why Class.forName for registering driver

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

Why doesn't java allow US to register a driver with a DriverManager and takes the Class.forName route?

Thanks in advance.
 
Saloon Keeper
Posts: 7585
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need to use Class.forName; it's perfectly possible to register a driver by calling DriverManager.registerManager explicitly. That means hard-coding the class name of the driver, though, which takes away some of the flexibility the Class.forName approach allows.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most Driver classes have static initialiser blocks which register the driver. Those are executed when the class is loaded, which you can do with Class.forName().
 
Mustafa Garhi
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you both.

We can still create the object of the Driver using reflection. So there would be the flexibility of having string driver name defined in the peroperty file.

I am sure this will be an overhead. So what i want to ask here is other than reducing the overhead is there any other advantage of going the Class.forName way?

Thanks again
Mustafa
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never underestimate the importance of convention.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Reflection would be much more difficult to achieve. Once the database supplier has written and tested the driver connections, you can create a link to the database in one line.
 
Mustafa Garhi
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear i couldn't understand your point. Can you please clarify which conventions are we talking about?

Thanks for all the patience.
 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each JDBC Driver class has static block definition where Driver registering with DriverManager logic have been implemented... As class first time is loaded by JVM, static block will be executed...

Invoking Class.forName() passing the JDBC Driver ClassName to be loaded, loads the JDBC Driver class and static block will be executed so that implicitly JDBC Driver gets registered with DriverManager...

So, overhead of making Driver registration in DriverManager list manually is avoided...

You go and explore the code in JDBC Driver class...
 
Ram Narayan.M
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And also its something an encapsulation... Client program doing JDBC activities does'nt need to involve in writing the core logic of registering with DriverManager...

So is Class.forName() approach...

Our Prime focus is to use JDBC only to fetch connections and execute SQL statements thats all...
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mustafa Garhi wrote:Bear i couldn't understand your point.


Just because you can do something in an odd and unconventional way doesn't mean that you should. Doing things in "clever" ways doesn't make you look clever -- they make you look arrogant and pompous. Experienced engineers follow conventions so that their code is clear, readable and understandable.

Mustafa Garhi wrote:Can you please clarify which conventions are we talking about?


While there are other ways to accomplish the task, using Class.forName() is the accepted convention. So unless you have a sound engineering reason to do it another way, following the convention is best.
 
Mustafa Garhi
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys. I agree completely with you Bear.
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://download.oracle.com/javase/tutorial/jdbc/basics/connecting.html


n previous versions of JDBC, to obtain a connection, you first had to initialize your JDBC driver by calling the method Class.forName. This methods required an object of type java.sql.Driver. Each JDBC driver contains one or more classes that implements the interface java.sql.Driver. The drivers for Java DB are org.apache.derby.jdbc.EmbeddedDriver and org.apache.derby.jdbc.ClientDriver, and the one for MySQL Connector/J is com.mysql.jdbc.Driver. See the documentation of your DBMS driver to obtain the name of the class that implements the interface java.sql.Driver.

Any JDBC 4.0 drivers that are found in your class path are automatically loaded. (However, you must manually load any drivers prior to JDBC 4.0 with the method Class.forName.)



 
Greenhorn
Posts: 13
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:


Any JDBC 4.0 drivers that are found in your class path are automatically loaded. (However, you must manually load any drivers prior to JDBC 4.0 with the method Class.forName.)





Finally some up to date information!
 
You guys haven't done this much, have ya? I suggest you study this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic