• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reg class.forName()

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a read through our JDBC FAQs. This question get asked a lot.
 
swapna hyderabad
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 1143
1
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

"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.
 
swapna hyderabad
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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......
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at the docs .
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic