The traditional process of using JDBC to access a database is:
Class.forName ("Com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/test?user=root&password=123456″;
Connection con = drivermanager.getconnection (URL);
Statement Statement = Con.createstatement ();
At the beginning of the use of the time, do not understand why the first to load a driver class, then you can get the connection, very curious drivermanager is how to get the information of the driver class, Later saw the next com.mysql.jdbc.Driver this class of source code, enlightened. It turns out that there is a static initialization code in the Com.mysql.jdbc.Driver class:
static {
try {
Java.sql.DriverManager.registerDriver (New Driver ());
} catch (SQLException E) {
throw new RuntimeException ("Can ' t Register driver!");
}
}
That is, when Class.forName loads the driver class and starts executing the static initialization code, it automatically creates a new driver object and calls Drivermanager.registerdriver to register itself in DriverManager.
PS1: Class.forName (string) differs from Classloader.loadclass (string)
Class.forName (String): Loads the class and performs class initialization; the second parameter can be Class.forName (string, Boolean, ClassLoader) to simply load a class without initializing;
Classloader.loadclass (String): Loads the class only, does not perform class initialization;
PS2: Sometimes you see this usage:
Class.forName ("Com.mysql.jdbc.Driver"). newinstance ();
This is not necessary, as described above, static initialization has been new to a driver object, register to DriverManager, and then build a driver object is completely unnecessary, waste space.
PS3: combined with ps1,class.forname ("Com.mysql.jdbc.Driver"), equivalent to:
ClassLoader loader = Thread.CurrentThread (). Getcontextclassloader ();
Class cls = Loader.loadclass ("Com.mysql.jdbc.Driver");
Cls.newinstance ();
The problem of this method with PS2, wasted a driver object;
PS4: in Java 6, the concept of service provider is introduced, that is, the service can be configured in a configuration file (which may be a interface or abstract Class) (that is, the service implementation provider). The configuration path is:/meta-inf/services/below. For more information, see: http://docs.oracle.com/javase/6/docs/technotes/guides/jar/jar.html#service%20provider
Java.sql.DriverManager also added support for this, so in JDK6, DriverManager's lookup driver range is:
1) The driver value configured in the System property "Jdbc.drivers";
2) User calls Class.forName () registered driver
3) The driver value configured in the service provider configuration file Java.sql.Driver.
Therefore, in jdk6, you can actually load the MySQL driver without calling Class.forName, because the MySQL driver jar package already contains the Java.sql.Driver the configuration file and added Com.mysql.jdbc.Driver to the file. But before the JDK6 version, this method is still called.
Reference Documentation:
1) http://docs.oracle.com/javase/1.5.0/docs/api/java/sql/DriverManager.html
2) http://docs.oracle.com/javase/6/docs/api/index.html?java/sql/DriverManager.html
3) Http://docs.oracle.com/javase/6/docs/technotes/guides/jar/jar.html#Service%20Provider
Class.forName ("Com.mysql.jdbc.Driver")