aspose file tools*
The moose likes JDBC and the fly likes What do Class.forName( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "What do Class.forName("Driver"); do?" Watch "What do Class.forName("Driver"); do?" New topic
Author

What do Class.forName("Driver"); do?

Selvarasu Ramasamy
Greenhorn

Joined: Oct 17, 2006
Posts: 1
Class.forName("com.mysql.jdbc.Driver"); // Registers driver with DerverManager

Why this is not like this?
import com.mysql.jdbc.Driver;
...
Driver mysqlDriver = new Driiver();
DriverManager.setDriver(mysqlDriver);

Can someone explain to me?


Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31054
    
162

Selvarasu,
Welcome to CodeRanch!

It's commonly written that way because the driver might not be available at compile time or you switch drivers. For example, suppose you are working with an embedded database locally and Oracle in Production. You don't want to have to recompile the code.

This turns out to not be a problem much in practice as many applications look up the DataSource from the JNDI (in Tomcat, WebSphere etc) and don't need Class.forName() either.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
 
 
subject: What do Class.forName("Driver"); do?