469,306 Members | 2,542 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

class.forName vs DriverManager.registerDriver

Dear Friend
Please explain the topic---

which one is better to use class.forName or DriverManager.registerDriver at the time when we want to get a connection with database.
Please explain it..


And please also explain which one is dynamic?

Thanks and regards

Debabrata
Aug 31 '07 #1
7 10428
r035198x
13,262 8TB
Dear Friend
Please explain the topic---

which one is better to use class.forName or DriverManager.registerDriver at the time when we want to get a connection with database.
Please explain it..


And please also explain which one is dynamic?

Thanks and regards

Debabrata
Class.forName() registers the JDBC driver by calling DriverManager.registerDriver(). It also creates an instance of the driver. There is no need to call DriverManager.registerDriver() unless of course you are writing the driver itself.
Aug 31 '07 #2
dmjpro
2,476 2GB
Dear Friend
Please explain the topic---

which one is better to use class.forName or DriverManager.registerDriver at the time when we want to get a connection with database.
Please explain it..


And please also explain which one is dynamic?

Thanks and regards

Debabrata
What do you mean by Dynamic?
Be specific.

Kind regards,
Dmjpro.
Aug 31 '07 #3
JosAH
11,448 Expert 8TB
Class.forName() registers the JDBC driver by calling DriverManager.registerDriver(). It also creates an instance of the driver. There is no need to call DriverManager.registerDriver() unless of course you are writing the driver itself.
Class.forName() doesn't do anything like that, it just loads the mentioned class.
Some JDBC drivers register themselves when their static { ... } class initialization
code is invoked; other JDBC drivers don't do that and you have to register such
driver yourself. Note that both these variants are outdated (see the API docs).

kind regards,

Jos
Aug 31 '07 #4
r035198x
13,262 8TB
Class.forName() doesn't do anything like that, it just loads the mentioned class.
Some JDBC drivers register themselves when their static { ... } class initialization
code is invoked; other JDBC drivers don't do that and you have to register such
driver yourself. Note that both these variants are outdated (see the API docs).

kind regards,

Jos
Of course Class.forName when called simply loads the mentioned class, but when called on a driver class the static initializer of that class would then perform the other actions I was talking about. So in the context of drivers and when comparing Class.forName with DriverManager.registerDriver .... ugh, I'm just trying to defend myself here ....

From this page they have :

"All Driver classes should be written with a static section (a static initializer) that creates an instance of the class and then registers it with the DriverManager class when it is loaded.
....


A Driver class is loaded, and therefore automatically registered with the DriverManager, in one of two ways:
  1. by calling the method Class.forName. This explicitly loads the driver class. Since it does not depend on any external setup, this way of loading a driver is the recommended one for using the DriverManager framework. The following code loads the class acme.db.Driver:
Class.forName("acme.db.Driver");
If acme.db.Driver has been written so that loading it causes an instance to be created and also calls DriverManager.registerDriver with that instance as the parameter (as it should do), then it is in the DriverManager's list of drivers and available for creating a connection.
.....
"
Aug 31 '07 #5
JosAH
11,448 Expert 8TB
No need to defend yourself, I wasn't attacking you ;-) That JDBC framework went
through a bit of evolution too and nowaday drivers are required to register themselves.
In the 'old' days it was up to the vendor how you were supposed to get their driver
registered. Nowadays we have DataSources so we have to go through all those
new hoops to get to our own data ;-)

kind regards,

Jos
Aug 31 '07 #6
r035198x
13,262 8TB
No need to defend yourself, I wasn't attacking you ;-) That JDBC framework went
through a bit of evolution too and nowaday drivers are required to register themselves.
In the 'old' days it was up to the vendor how you were supposed to get their driver
registered. Nowadays we have DataSources so we have to go through all those
new hoops to get to our own data ;-)

kind regards,

Jos
Did they really get the DataSources idea from M$ or was that somewhere in the road map?
Aug 31 '07 #7
JosAH
11,448 Expert 8TB
Did they really get the DataSources idea from M$ or was that somewhere in the road map?
I really don't know; I noticed that funny thing when I noticed a similar DataSource
way of installing your driver in the Apache project. It has to do with how the JNDI
stuff got popular; not sure though ... I'm not a database fanatic ;-)

kind regards,

Jos
Aug 31 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Sebastien Degardin | last post: by
1 post views Thread by JagPeram | last post: by
3 posts views Thread by vijayvksen | last post: by
4 posts views Thread by vasavimaruthi | last post: by
1 post views Thread by chanshaw | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.