aspose file tools*
The moose likes JDBC and the fly likes Re:Class.forName() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Re:Class.forName()" Watch "Re:Class.forName()" New topic
Author

Re:Class.forName()

Sujith Kanaparthi
Ranch Hand

Joined: Sep 04, 2005
Posts: 45
Hi ,

I have a query.The function forName(String className) of java.lang.Class returns class object associated with the given string.Every return type must be caught.But if I write a programm such as


public class Demo{
public static void main(String arg[]){
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");//this function returns class object
/* code to manipulate database */


}
}

Eventhough I have not caught the returned class object as Class m = Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"), the program working fine.I just want to know how is this possible.Please clarify my doubt.

Regards,
Sujith
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

A couple of small points: Classes don't need to be caught. Only checked Exceptions need caught. Also Java has methods not functions.

It is perfectly legal Java to call a method which returns a value and do nothing with the value. For example:

Will compile no problem, despite the fact that valueOf returns a Boolean.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

A 'side effect' of this method is that it causes the Class to be loaded, but only if it has not already been loaded. The trick here is that a Driver has a 'static block' which is a section of code that gets run when the class loads.

Therefore the forName() loads the Class, the static block fires, and the Driver registers itself with the DriverManager. This only gets done once, since the Class won't be loaded again.
Sujith Kanaparthi
Ranch Hand

Joined: Sep 04, 2005
Posts: 45
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

but what happens to the returned value of the above method call? where is it going to be stored ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42286
    
  64
It's not stored anywhere. Since there is no assignment, the compiler will discard the object.


Ping & DNS - my free Android networking tools app
Sujith Kanaparthi
Ranch Hand

Joined: Sep 04, 2005
Posts: 45
Thanks to all for clarifying my doubt.
Rashi Gulati
Ranch Hand

Joined: Jan 08, 2004
Posts: 44
Hi SUJITH SUNIL KANAPARTHI

Class.forName(jdbc dbc:YourDriver) actually work this way:
YourDriver is a class which has a static block in that static block you are creating the instance of YourDriver class which in turn will call the DriverManager.registerDriver and hence will get registerd with the DriverManager as DriverManager maintains the list of all the drivers registered with it.
Something like this
class YourDriver implements Driver
{
static
{
YourDriver d = new YourDriver();
DriverManager.registerDriver(d);
}
}

Instead of writing Class.forName(jdbc dbc:YourDriver) you can also write a command on the command prompt like this
jdbc.drivers = jdbc dbc:YourDriver :jdbc dbc:YourDriver2:jdbc dbc:YourDriver3
and so on by this you are actually adding the Drivers to the java.lang.System property so when the DriverManager class is intialized it looks for jdbc.drivers if user has entered drivers then it will load them, by this you can specify more then one driver seprated with :

I hope this will help
Take care
Rashi
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Instead of writing Class.forName(jdbc dbc:YourDriver) you can also write a command on the command prompt like this jdbc.drivers=jdbc:odbc:YourDriver:jdbc:odbc:YourDriver2:jdbc:odbc:YourDriver3

Not quite right.
You can put them as -D options, and they have to be the Driver class, not the JDBC URL eg

java -Djdbc.driver=oracle.jdbc.OracleDriver;com.next.jdbc.Driver com.your.class.to.Run
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61433
    
  67

SUJITH SUNIL KANAPARTHI,

Please do everyone a favor and adjust your display name to used mixed case rather than all uppercase, which is rather jarring.

thanks
bear
Forum bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Yes, I hope so. I've been delaying on commenting.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Re:Class.forName()