aspose file tools*
The moose likes JDBC and the fly likes JDBC Connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "JDBC Connection" Watch "JDBC Connection" New topic
Author

JDBC Connection

satish bora
Greenhorn

Joined: Jul 16, 2001
Posts: 18
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


Sa
ramalingam manonmani
Greenhorn

Joined: Aug 07, 2001
Posts: 1
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

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Jul 16, 2001
Posts: 18
Thanks Dave.
Cheers
Satish
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDBC Connection
 
Similar Threads
After loading many drivers how to use specific?
Class.forName()
connection pooling
what this method do in JDBC?
Dynamically loading a class