• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

JDBC Connection

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody let me know the difference between following two ways of connection?
1. DriverManager.registerDriver((Driver)Class.forName(JDBCD).newInstance());
2. Class.forName(jdbcDriver);
Is there any specific reason when to use one or other.
Thnx
Satish
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can i write odbc connectivity for javascripts.Should i need to write the code in java and then call that program in javascripts.Please suggest me.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll have a go at the second one (ramalingam manonmani) first: what you are probably looking for are JSPs and Servlets if you want to present database information in a web page...
Now to the initial question:
My preference has always been to use Class.forName("Driver class"). When the class is loaded, its static block (if it has one, and Drivers do have a static block) gets executed.
This allows the driver to register itself so that when you ask for a connection, the DriverManager can look to the registered Drivers and get one to return a Connection.
I'd imagine you could use the initial version when you had an instance of a driver already but did not load it or do not know how to load it yourself. Two ways in which this may occur is if the driver was streamed from some other site on the internet, or if the driver is not on your classpath and you loaded and instantiated it yourself... Pretty wierd but its possible.
As to the difference between Class.forName() and Class.forName().getInstance(), I've always prefered the initial version since it doesn't create a useless object. Then again, if you only ever creating one unused object in your code you're doing well so it isn't too much of a problem.
These are just thoughts so I wouldn't take them as fact but hope it helps anyway...
Dave.
 
satish bora
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dave.
Cheers
Satish
 
I like tacos! And this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic