JDBC Load Driver method

Source: Internet
Author: User

1.class.forname ("Com.mysql.jdbc.Driver");

2. Drivermanager.registerdriver (New Com.mysql.jdbc.Driver ());

3.system.setproperty ("Jdbc.drivers", "Com.mysql.jdbc.Driver");

Similarities and differences of Drivermanager.registerdriver and Class.forName ()

So what are the similarities and differences between these two ways? First we go to the DriverManager to see,

Java code
  1. public static synchronized void Registerdriver (Java.sql.Driver Driver)
  2. Throws SQLException {
  3. if (!initialized) {
  4. Initialize ();
  5. }
  6. Driverinfo di = new Driverinfo ();
  7. Di.driver = driver;
  8. Di.driverclass = Driver.getclass ();
  9. Di.driverclassname = Di.driverClass.getName ();
  10. Not Required--drivers.addelement (DI);
  11. Writedrivers.addelement (DI);
  12. println ("registerdriver:" + di);
  13. /* Update the read copy of drivers vector */
  14. Readdrivers = (java.util.Vector) writedrivers.clone ();
  15. }

Obviously, DriverManager encapsulates the driver information we need to register into a driverinfo into a writedrivers, This writedrivers is a static type vector variable declared in DriverManager. It will be used again when the getconnection.

So how does Class.forName ("Com.mysql.jdbc.Driver") register the driver, and we know that the primary function of Class.forName ("class name") is to instantiate a class instance to the virtual machine, Let's take a look at the source code of Com.mysql.jdbc.Driver.

Java code
  1. Public class Driver extends Nonregisteringdriver implements Java.sql.Driver {
  2. //~ Static fields/initializers
  3. // ---------------------------------------------  
  4. //  
  5. //Register ourselves with the DriverManager
  6. //  
  7. Static {
  8. try {
  9. Java.sql.DriverManager.registerDriver (new Driver ());
  10. } catch (SQLException E) {
  11. throw New RuntimeException ("Can ' t Register driver!");
  12. }
  13. }

In Com.mysql.jdbc.Driver, there is a static block of code that registers a driver instance with DriverManager. In this case, the Class.forName ("Com.mysql.jdbc.Driver") will first execute this static code block, and then Drivermanager.registerdriver (New Driver ()) Have the same effect.

So for both methods, here, I recommend using the second, the Class.forName ("Class name") way. There are two reasons.

1. When we execute Drivermanager.registerdriver (new Driver ()), the static code block has been executed, which is equivalent to instantiating two Driver objects.

2. Drivermanager.registerdriver (New Driver ()) produces a dependency on MySQL. And the way we class.forname can be changed dynamically when we run.

JDBC Load Driver method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.