| Author |
Confusion in Class.forName() :confused:
|
Glenny Dsilva
Ranch Hand
Joined: May 09, 2005
Posts: 42
|
|
I have read that Class.forName() is to load the class dynamically. And one of the places it is used is to load the Driver in jdbc. for e.g. Class.forName("com.mysql.jdbc.Driver"); But instead of the above code can i just use package com.mysql.jdbc.Driver; Why do i need to use Class.forName() (i.e. load it dynamically); Pls. explain
|
 |
Darryl Nortje
Ranch Hand
Joined: Jun 11, 2002
Posts: 140
|
|
--> I have read that Class.forName() is to load the class dynamically. --> And one of the places it is used is to load the Driver in jdbc. --> for e.g. --> Class.forName("com.mysql.jdbc.Driver"); Normally what you will do is have the driver in a properties file and then call Class.forName(myDriverProperty); That way by changing the value in the properties file you will be loading a different jdbc driver --> But instead of the above code can i just use --> package com.mysql.jdbc.Driver; I think you mean import com.mysql.jdbc.Driver ?? so what do you do if you now want to use a postgres database driver. You have to change and recompile your code to now import org.postgresql.Driver. --> Why do i need to use Class.forName() (i.e. load it dynamically); That's exactly why you need Class.forName(). To load the class dynamically. IE: No recompile necessary if you change databases. cheers Darryl
|
 |
KasiMurugan Ramasamy
Ranch Hand
Joined: Jan 30, 2005
Posts: 125
|
|
Hi guy, import doesn't load the class until your first reference that class.
|
Thanks & Regards
Kasimurugan (SCJP1.4, SCBCD1.3), Preparing SCWCD1.4
|
 |
 |
|
|
subject: Confusion in Class.forName() :confused:
|
|
|