Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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();
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I was wrong, regarding, registering driver & that sort of stuff ! right ?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The driver is registered by the driver itself when it is loaded into the VM.
 
rani bedi
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic