473,241 Members | 1,973 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

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

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

Similar topics

2
by: Sebastien Degardin | last post by:
Hello, I need to use a Factory pattern to create services class. i have an interface --> MyService i can have an abstract class --> MyAbstractService i have several concrete class for this...
1
by: JagPeram | last post by:
how to register a driver class? i need to connect to oracle db using java, here is my code, Connection con = null; String url = "jdbc:oracle:thin:@localhost:1521:"; ...
3
by: vijayvksen | last post by:
Hi I am using the following code to connect with oracle driver Class.forName("oracle.jdbc.driver.OracleDriver");...
4
by: PaperPilot | last post by:
I have a Java program that is deployed as a .jar file in which is another .jar file with the mysql library. Here is the appropriate section of code: /** * Makes the connection to the...
4
by: vasavimaruthi | last post by:
hi, i got class not found exception of a small jdbc program in windows. the program is like this import java.sql.*; public class Db1 { public static void main(String a)...
2
by: sdanda | last post by:
Hi , Do you have any idea how to improve my java class performance while selecting and inserting data into DB using JDBC Connectivity ......... This has to work for more than 8,00,000...
1
by: chanshaw | last post by:
Hey I'm trying to execute a simple statement but im getting a java.sql.Statement is abstract; cannot be instantiated. I just want to execute a simple select statement import...
2
by: whitep8 | last post by:
Hi All, The code at the bottom of this message is an example (1 of 3 i have tried) of trying to connect to a localhost mysql database. It wont, and the exception displayed is the one i have...
0
by: asti1987 | last post by:
I have a big problem, y need to print a jasper report, but it must to be with jsp. this is my jsp please help me <%@ page...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.