• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

What does Class.forName do?

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that
Class.forName(driver) loads driver to the system.
What exactly this command do. Can anyone elaborate it a bit.

Secondly, If I write
Class.forName(Driver1);
Class.forName(Driver2);
Class.forName(Driver3);
Class.forName(Driver4);

then, which driver will be used when I make a query to database.
Please explain, if possible.

Thanks,
Abra.
 
Ranch Hand
Posts: 1087
Oracle Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the Sun's JDBC Guide at http://java.sun.com/j2se/1.4.2/docs/guide/jdbc/getstart/drivermanager.html

  • by calling the method Class.forName. This explicitly loads the driver class. Since it does not depend on any external setup, this way of loading a driver is the recommended one for using the DriverManager framework.


  • If a has been written so that loading it causes an instance to be created and also calls DriverManager.registerDriver with that instance as the parameter (as it should do), then it is in the DriverManager's list of drivers and available for creating a connection.
  • It may sometimes be the case that more than one JDBC driver is capable of connecting to a given URL. For example, when connecting to a given remote database, it might be possible to use a JDBC-ODBC bridge driver, a JDBC-to-generic-network-protocol driver, or a driver supplied by the database vendor. In such cases, the order in which the drivers are tested is significant because the DriverManager will use the first driver it finds that can successfully connect to the given URL.


  • First the DriverManager tries to use each driver in the order it was registered. (The drivers listed in jdbc.drivers are always registered first.) It will skip any drivers that are untrusted code unless they have been loaded from the same source as the code that is trying to open the connection.

    It tests the drivers by calling the method Driver.connect on each one in turn, passing them the URL that the user originally passed to the method DriverManager.getConnection. The first driver that recognizes the URL makes the connection.




    Shailesh
     
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The method forName() is static method of Class,when call it,
    it can load class from your classpath,

    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver") // only load class
    you can load many drivers ,but which db u connecting , look follow code
    String url="jdbc:microsoft:sqlserver://10.41.30.148:1433;DatabaseName=caf_pm_pmp_db";
    Connection conn= DriverManager.getConnection(url,"sa",""); // decide which db you connecting
    [ June 26, 2006: Message edited by: Han Dian ]
     
    Greenhorn
    Posts: 8
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Class is class and forName() is static method.
    It loads the driver.
     
    Greenhorn
    Posts: 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi,

    The direct way to register the driver is,
    DriverManager.registerDriver(new OracleDriver);//just an example

    What Class.forName("Driver") does is, It will register the driver by calling the same method DriverManager.registerDriver(new OracleDriver);

    See the below example,

    Class SampleDriver{
    SampleDriver(){}
    static{
    DriverManager.registerDriver(new SampleDriver());
    }
    }

    Note: As you all know, The static block will be called when we load a class. That means when we put Class.forName("SampleDriver") the static block will be called where the DriverManager.registerDriver(new SampleDriver()) is invoked.

    I hope this will help you..

    Regards,
    Chandra M
     
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi friends,
    First thing is that Class.forName("class_name");
    loads the class in Java Virtual Machine.
    Here where it loads is important.
    in your case it loads the driver_class;
    second thing is that if you load multiple drivers i.e driver1,driver2 etc.
    it doesnot matter.It uses only one.
    Because one more important thing is that
    while getting connection which url you use.
    String url="JDBC:ODBC:MYDSN";
    e.g.Connection con=DriverManager.getConnection(url,"user","password");
     
    Ranch Hand
    Posts: 111
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Why is manual stuff like :

    MyDriver driver = new MyDriver() ( OR MyDriver.getInstance() );
    DriverManager.registerDriver(driver);

    Not recommended over Class.forName("MyDriver") if they do the same thing?
     
    Ranch Hand
    Posts: 5575
    Eclipse IDE Windows XP Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Welcome to Javaranch Chandra Mayamuthu
     
    Mustafa Garhi
    Ranch Hand
    Posts: 111
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Chandra i have been using javaranch since quite a good time, anyway thanks for you thoughtful welcome

    And yes i had a doubt which i raised in my last post
     
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Why is manual stuff like :

    MyDriver driver = new MyDriver() ( OR MyDriver.getInstance() );
    DriverManager.registerDriver(driver);

    Not recommended over Class.forName("MyDriver") if they do the same thing?


    It's preferred in order to keep the code flexible. Keeping the driver class name, DB URL, DB username and DB password outside of the code makes it much easier to change those, which come shandy during development (when you may want to use a local DB like MYSQL or Postgres instead of SQLServer or Oracle).
     
    Ranch Hand
    Posts: 32
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Class.forName("class name");
    is generlly used to load a class specified.
    and then internally this method call the method a static block of specified class as follows -

    DriverManager.registerDriver(new Driver());

    then it creates object of the specified Driver.

     
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    santosh kimothi wrote:Class.forName("class name");
    is generlly used to load a class specified.
    and then internally this method call the method a static block of specified class as follows -

    DriverManager.registerDriver(new Driver());

    then it creates object of the specified Driver.



    1. Driver myDriver = new MyDriver();
    2. DriverManager.registerDriver(myDriver);

    will register the same driver twice by creating two instances of the same MyDriver, because while doing ne MyDriver() internally it will load the MyDriver class that's where it encounters static block

    A. static {
    DriverManager.registerDriver(myDriver);
    }

    there-after it comes to your piece of code i.e. (1) & (2) above whereby you are just trying to create another instance & register the second that instance with DriverManager. So, now we have two instances of the same driver registered & as code inside static block in (A) above registers it first while loading the MyDriver class so DriverManager will always pick it up for connectivity & your 'myDriver" in (1) & (2) are never invoked.

    So, to conclude doing "new MyDriver()" doesn't add any value instead it registers duplicate driver with DriverManager which will never be used. That is why preferred approach laid down is --
    Class.forName("org.jdbc.myDriver");
     
    Marshal
    Posts: 80288
    433
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Welcome to JavaRanch
     
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    May I know does loads the driver class means import the driver class using import statement?
     
    Campbell Ritchie
    Marshal
    Posts: 80288
    433
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Fei Wong wrote:May I know does loads the driver class means import the driver class using import statement?

    No, it doesn't

    An import statement tells the compiler which class to look for. Class#forName() instructs the Class<?> class to find a class-loader and load that particular Class<?> object into the memory used by the JVM. A fuller description about how that registers connections and bridges and drivers has already been given in this thread.
     
    Hey, I'm supposed to be the guide! Wait up! No fair! You have the tiny ad!
    Smokeless wood heat with a rocket mass heater
    https://woodheat.net
    reply
      Bookmark Topic Watch Topic
    • New Topic