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 Why do we 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 "Why do we do Class.forName(....) ?" Watch "Why do we do Class.forName(....) ?" New topic
Author

Why do we do Class.forName(....) ?

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
To the parameter of Class.forName, we give give name of abc.class file, as 'abc'. As i know with that, it returns object associated with it.
Instead of it, why dont we simply get its object by
new abc()?
As i know is, with Class.forName , you register that driver .
Is it true ? or there are more secrets to this concept ?
from,
vikram.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
From the Sun JDC: http://developer.java.sun.com/developer/qow/archive/114/index.html
The purpose of the JDBC API is to make it easy to use other drivers. For example, the driver can be passed into a application using a command line system property. The pseudo code for this could look like this:
String driver = System.getProperties().getProperties("db.driver");
Class.forName(driver);
The application runs like this:
java -Ddb.driver=abc.def.SybDriver MyApp
Alternatively, you do not need to load a driver dynamically. You can load it like any other class:
import abc.def.MyDriver;
MyDriver myDriver = new MyDriver();

Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
So I was wrong, regarding, registering driver & that sort of stuff ! right ?
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
The driver is registered by the driver itself when it is loaded into the VM.
rani bedi
Ranch Hand

Joined: Feb 06, 2001
Posts: 358
Hi,
In a JDBC program, one of the first things to do is to load the JDBC driver by calling the forName() static method of the Class class. forName() takes one string parameter: the name of the driver along with its package. For JavaSoft's JDBC-ODBC Bridge, this string is "sun.jdbc.odbc.JdbcOdbcDriver". Therefore, the call would look like:
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");


Cheers,<br />Rani<br />SCJP, SCWCD, SCBCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why do we do Class.forName(....) ?