7 Replies Latest reply: Mar 14, 2008 11:39 AM by 807601 RSS

    Loading JDBC driver

    807601
      Q: Which of the following ways will not load a JDBC Driver?

      A.Configure the list of drivers in jdbc.driver system property

      B.call the Driver's constructor

      C.pass class name to forName method of the class

      D.use JNDI to locate the Driver
        • 1. Re: Loading JDBC driver
          807601
          Before a connection to a database can be established, the JDBC driver for that database must be loaded. Drivers automatically register themselves with the JDBC system when loaded. There are two ways to load a JDBC driver. The first is to specify the driver or colon-separated list of drivers on the command line:

          > java -Djdbc.drivers=com.company1.Driver:com.company2.Driver MyApp


          The second, and recommended method, is to call Class.forName() within the code:


          try {
          // Load the JDBC driver
          String driverName = "org.gjt.mm.mysql.Driver";
          Class.forName(driverName);
          } catch (ClassNotFoundException e) {
          // Could not find the driver
          }




          So I think option B and D will not load Jdbc Driver
          Regards,

          Snehanshu....
          • 2. Re: Loading JDBC driver
            807601
            If making the JVM load the class through Class.forName registers the driver, constructing a driver instance will register it too.
            • 3. Re: Loading JDBC driver
              807601
              BIJ001 wrote:
              If making the JVM load the class through Class.forName registers the driver, constructing a driver instance will register it too.
              ... And even if it would not register (if it were worse than the example below), you could still use it after constructing an instance
              import java.sql.*;
              public class WhyRegister {
                   public static void main(String[] args) {
                        try {
                             Driver myDriver = new com.mysql.jdbc.Driver();
                             Connection c = myDriver.connect(
                                       "jdbc:mysql://localhost/TestDB", null);
                             //...
                             c.close();
                        } catch (SQLException e) {
                             System.out.println(e.getMessage());
                        }
                   }
              }
              Option B definitely should work as well...
              • 4. Re: Loading JDBC driver
                807601
                DataFiddler wrote:
                BIJ001 wrote:
                If making the JVM load the class through Class.forName registers the driver, constructing a driver instance will register it too.
                ... And even if it would not register (if it were worse than the example below), you could still use it after constructing an instance
                import java.sql.*;
                public class WhyRegister {
                     public static void main(String[] args) {
                          try {
                               Driver myDriver = new com.mysql.jdbc.Driver();
                               Connection c = myDriver.connect(
                                         "jdbc:mysql://localhost/TestDB", null);
                               //...
                               c.close();
                          } catch (SQLException e) {
                               System.out.println(e.getMessage());
                          }
                     }
                }
                Option B definitely should work as well...
                But if your runtime can instantiate a class without loading it, you've got something very odd indeed happening.....
                • 5. Re: Loading JDBC driver
                  807601
                  Look, this is not homework help!
                  • 6. Re: Loading JDBC driver
                    807601
                    01-troll-10 wrote:
                    Look, this is not homework help!
                    Nor is it your playground. Run along now. Don't forget the scissors
                    • 7. Re: Loading JDBC driver
                      807601
                      georgemc wrote:
                      ... instantiate a class without loading it ...
                      Studying ClassLoader stuff is very valuable, I agree ;)