File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes What do Class.forName( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "What do Class.forName("Driver"); do?" Watch "What do Class.forName("Driver"); do?" New topic

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

Selvarasu Ramasamy

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();

Can someone explain to me?

Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33130

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.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: What do Class.forName("Driver"); do?
It's not a secret anymore!