• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Selvarasu Ramasamy
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34686
367
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic