class.forName vs DriverManager.registerDriver

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Debabrata Jana
    New Member
    • Mar 2007
    • 8

    class.forName vs DriverManager.registerDriver

    Dear Friend
    Please explain the topic---

    which one is better to use class.forName or DriverManager.r egisterDriver at the time when we want to get a connection with database.
    Please explain it..


    And please also explain which one is dynamic?

    Thanks and regards

    Debabrata
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Debabrata Jana
    Dear Friend
    Please explain the topic---

    which one is better to use class.forName or DriverManager.r egisterDriver at the time when we want to get a connection with database.
    Please explain it..


    And please also explain which one is dynamic?

    Thanks and regards

    Debabrata
    Class.forName() registers the JDBC driver by calling DriverManager.r egisterDriver() . It also creates an instance of the driver. There is no need to call DriverManager.r egisterDriver() unless of course you are writing the driver itself.

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      Originally posted by Debabrata Jana
      Dear Friend
      Please explain the topic---

      which one is better to use class.forName or DriverManager.r egisterDriver at the time when we want to get a connection with database.
      Please explain it..


      And please also explain which one is dynamic?

      Thanks and regards

      Debabrata
      What do you mean by Dynamic?
      Be specific.

      Kind regards,
      Dmjpro.

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by r035198x
        Class.forName() registers the JDBC driver by calling DriverManager.r egisterDriver() . It also creates an instance of the driver. There is no need to call DriverManager.r egisterDriver() unless of course you are writing the driver itself.
        Class.forName() doesn't do anything like that, it just loads the mentioned class.
        Some JDBC drivers register themselves when their static { ... } class initialization
        code is invoked; other JDBC drivers don't do that and you have to register such
        driver yourself. Note that both these variants are outdated (see the API docs).

        kind regards,

        Jos

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by JosAH
          Class.forName() doesn't do anything like that, it just loads the mentioned class.
          Some JDBC drivers register themselves when their static { ... } class initialization
          code is invoked; other JDBC drivers don't do that and you have to register such
          driver yourself. Note that both these variants are outdated (see the API docs).

          kind regards,

          Jos
          Of course Class.forName when called simply loads the mentioned class, but when called on a driver class the static initializer of that class would then perform the other actions I was talking about. So in the context of drivers and when comparing Class.forName with DriverManager.r egisterDriver .... ugh, I'm just trying to defend myself here ....

          From this page they have :

          "All Driver classes should be written with a static section (a static initializer) that creates an instance of the class and then registers it with the DriverManager class when it is loaded.
          ....


          A Driver class is loaded, and therefore automatically registered with the DriverManager, in one of two ways:
          1. 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. The following code loads the class acme.db.Driver:
          Class.forName(" acme.db.Driver" );
          If acme.db.Driver has been written so that loading it causes an instance to be created and also calls DriverManager.r egisterDriver 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.
          .....
          "

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            No need to defend yourself, I wasn't attacking you ;-) That JDBC framework went
            through a bit of evolution too and nowaday drivers are required to register themselves.
            In the 'old' days it was up to the vendor how you were supposed to get their driver
            registered. Nowadays we have DataSources so we have to go through all those
            new hoops to get to our own data ;-)

            kind regards,

            Jos

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by JosAH
              No need to defend yourself, I wasn't attacking you ;-) That JDBC framework went
              through a bit of evolution too and nowaday drivers are required to register themselves.
              In the 'old' days it was up to the vendor how you were supposed to get their driver
              registered. Nowadays we have DataSources so we have to go through all those
              new hoops to get to our own data ;-)

              kind regards,

              Jos
              Did they really get the DataSources idea from M$ or was that somewhere in the road map?

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by r035198x
                Did they really get the DataSources idea from M$ or was that somewhere in the road map?
                I really don't know; I noticed that funny thing when I noticed a similar DataSource
                way of installing your driver in the Apache project. It has to do with how the JNDI
                stuff got popular; not sure though ... I'm not a database fanatic ;-)

                kind regards,

                Jos

                Comment

                Working...