aspose file tools*
The moose likes JDBC and the fly likes reg class.forName() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "reg class.forName()" Watch "reg class.forName()" New topic
Author

reg class.forName()

swapna hyderabad
Greenhorn

Joined: Nov 20, 2008
Posts: 20
Hi,

to load the driver class we use class.forName("class") what it will do?
can't we create driver class object using "new" key word?? waht is the difference?? Please explain me in detail.......

Thanks.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Have a read through our JDBC FAQs. This question get asked a lot.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
swapna hyderabad
Greenhorn

Joined: Nov 20, 2008
Posts: 20
Originally posted by Paul Sturrock:
Have a read through our JDBC FAQs. This question get asked a lot.


Hi,
I am no where find this question in above link..please provide me the answer
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1135

Swapna,
"forName()" also registers the driver with the "DriverManager" class, which "new" does not do.
So if you use "new", you also have to ensure, in your code, that you explicitly register the driver with "DriverManager".

Good Luck,
Avi.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42600
    
  65
"forName()" also registers the driver with the "DriverManager" class, which "new" does not do.
So if you use "new", you also have to ensure, in your code, that you explicitly register the driver with "DriverManager".

This is not correct. The driver registers itself with the DriverManager when the class gets loaded (that's done in a static initialization block). Since the class must be loaded in order to instantiate it, the registration will happen without anything needing to be done by the user code. See the javadocs for java.sql.Driver for more information.


Ping & DNS - my free Android networking tools app
swapna hyderabad
Greenhorn

Joined: Nov 20, 2008
Posts: 20
Thanks for the reply

but i didn't get the answer:-(,please provide me the difference and why we will use class.for.Name() not in JDBC generally......
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Take a look at the docs .


SCJP, SCWCD.
|Asking Good Questions|
ashwini allamsetti
Greenhorn

Joined: Oct 26, 2008
Posts: 3
hi,
Its simple basically..
check the syntax out : Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
is not it?
So over there forName() is a static method available in the class called Class and the String argument shows the path where the drivers are available..
and the answer for your question is here..
since forName() is a static method otherwise a class method..what all we don't need is to create an object...object creation essentially requires heap area to be allocated and this case object creation is meaningless for a static method.

Comments for my answer are most welcome..


Ashwini Kumar Allamsetti
SCJP
 
jQuery in Action, 2nd edition
 
subject: reg class.forName()