File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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 Murach's Java Servlets and JSP this week in the Servlets 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
internet detective
Marshal

Joined: May 26, 2003
Posts: 30076
    
149

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What do Class.forName("Driver"); do?
 
Similar Threads
mysql JDBC driver classpath problem.
could not locate driver
Writing to MySql Database
Confusion in Class.forName() :confused:
MySQL drivers