• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why Class.forName for registering driver

 
Mustafa Garhi
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • 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.
 
Tim Moores
Bartender
Posts: 2839
46
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never underestimate the importance of convention.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you guys. I agree completely with you Bear.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18212
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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.)


 
Horst Stieg
Greenhorn
Posts: 13
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • 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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic