4 Replies Latest reply: Sep 26, 2012 2:41 PM by jschellSomeoneStoleMyAlias RSS

    db connection using Class.forName()

    861516

      Hi,

      I have a doubt regarding the working of Class.forName() method.
      How exactly it works in jvm?
      What is the difference between
      Class.forName("oracle.jdbc.driver.OracleDriver") & DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver())?
      If my JDBC code is something like this:
      Class.forName("oracle.jdbc.driver.OracleDriver");
      Class.forName("Some other driver class");
      Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@hostname:1521:SID","user", "pwd");


      Which class will be loaded and how will DriverManager class will know which driver to communicate?
      Can anybody help me on this?


      Thanks in advance.

      Edited by: TuhinC on Sep 26, 2012 11:41 AM

        • 1. Re: db connection using Class.forName()
          Kayaman
          TuhinC wrote:
          I have a doubt regarding the working of Class.forName() method.
          How exactly it works in jvm?
          Well, you might want to read the javadocs for Class.forName() and if you still think you need more information, go for the specifications.
          What is the difference between
          Class.forName("oracle.jdbc.driver.OracleDriver") & DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver())?
          In the first case the class needs to be only on the runtime classpath, in the latter case the class needs to be on the compile-time classpath too.
          If my JDBC code is something like this:
          Class.forName("oracle.jdbc.driver.OracleDriver");
          Class.forName("Some other driver class");
          Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@hostname:1521:SID","user", "pwd");


          Which class will be loaded and how will DriverManager class will know which driver to communicate?
          Both classes will be loaded, as you're calling forName. The database connection string is specific to a database, so DriverManager finds the correct driver based on that.

          Also apparently you don't even need the Class.forName() anymore to load the driver. At least I seem to remember someone mentioning that in a different thread.
          • 2. Re: db connection using Class.forName()
            handat
            >
            Also apparently you don't even need the Class.forName() anymore to load the driver. At least I seem to
            remember someone mentioning that in a different thread.
            This is because DriverManager does it for you, but in a smarter way using a classloader so it can also unload the class dynamically and reload another one.
            • 3. Re: db connection using Class.forName()
              Kayaman
              handat wrote:
              This is because DriverManager does it for you, but in a smarter way using a classloader so it can also unload the class dynamically and reload another one.
              So I heard, but that's not how it used to be. Then again, the last time I've been using DriverManager to get connections is quite a while back.
              • 4. Re: db connection using Class.forName()
                jschellSomeoneStoleMyAlias
                handat wrote:
                Also apparently you don't even need the Class.forName() anymore to load the driver. At least I seem to
                remember someone mentioning that in a different thread.
                This is because DriverManager does it for you, but in a smarter way
                Except of course that if it fails to load you will have no idea but instead you will get an error about your connection string.
                using a classloader so it can also unload the class dynamically and reload another one.
                It does? Do you have a reference for that?