aspose file tools*
The moose likes JDBC and the fly likes Why Class.forName for registering driver Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Why Class.forName for registering driver" Watch "Why Class.forName for registering driver" New topic
Author

Why Class.forName for registering driver

Mustafa Garhi
Ranch Hand

Joined: Nov 05, 2008
Posts: 111
Hi,

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

Thanks in advance.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
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

Joined: Nov 05, 2008
Posts: 111
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
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61603
    
  67

Never underestimate the importance of convention.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39784
    
  28
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

Joined: Nov 05, 2008
Posts: 111
Bear i couldn't understand your point. Can you please clarify which conventions are we talking about?

Thanks for all the patience.
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

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...


SCJP 6 [SCJP - Old is Gold]
Ram Narayan.M
Ranch Hand

Joined: Jul 11, 2010
Posts: 247

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61603
    
  67

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

Joined: Nov 05, 2008
Posts: 111
Thank you guys. I agree completely with you Bear.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16228
    
  21

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.)



Customer surveys are for companies who didn't pay proper attention to begin with.
Horst Stieg
Greenhorn

Joined: Oct 14, 2011
Posts: 13

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why Class.forName for registering driver