Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDBC Connection

 
satish bora
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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
 
ramalingam manonmani
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks Dave.
Cheers
Satish
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic