473,386 Members | 1,754 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,386 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 10830
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.