Forums Register Login

Why Class.forName for registering driver

+Pie Number of slices to send: Send
Hi,

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

Thanks in advance.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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().
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Never underestimate the importance of convention.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
Bear i couldn't understand your point. Can you please clarify which conventions are we talking about?

Thanks for all the patience.
+Pie Number of slices to send: Send
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...
+Pie Number of slices to send: Send
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...
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Thank you guys. I agree completely with you Bear.
+Pie Number of slices to send: Send
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.)



+Pie Number of slices to send: Send
 

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'm not sure if I approve of this interruption. But this tiny ad checks out:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1851 times.
Similar Threads
Loading Oracle JDBC Driver
Loading Oracle JDBC Driver
Class.forName
Loading a JDBC Driver
How to connect MySQL DB from JSP?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:48:13.