class.forname & classloader

From https://www.cnblogs.com/gaojing/archive/2012/03/15/2413638.html

The traditional use jdbc to access the database process:

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();

 

The very beginning of use, do not understand why we must first load a driver class, then you can get a Connection, and very curious about is how to get information that DriverManager driver class, and later looked at this class com.mysql.jdbc.Driver source code, suddenly see the light. So that there are some static initialization code in 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 the driver has been loaded Class.forName classes begin the static initialization code, it will automatically create a new object Driver and call DriverManager.registerDriver themselves registered with the DriverManager go.

PS1:  the Class.forName (String) and ClassLoader.loadClass (String) distinction
Class.forName (String): class loading, and performs class initialization; by Class.forName (String, boolean, ClassLoader) the second parameter only no initialization loaded class;
ClassLoader.loadClass (String): only load classes, class initialization is not performed;

ps2:  sometimes you see this usage:
Class.forName ( "com.mysql.jdbc.Driver") newInstance ();.
It is not necessary, as mentioned above, the static initialization of new object has a Driver's registered to DriverManager go in, and then create a Driver in this subject is completely unnecessary, a waste of space.

PS3:  binding ps1, Class.forName ( "com.mysql.jdbc.Driver") ; corresponds to:

ClassLoader loader = Thread.currentThread().getContextClassLoader();
Class cls = loader.loadClass(“com.mysql.jdbc.Driver”);
cls.newInstance();

 


The problem with this approach with ps2, wasted a Driver objects;

ps4:  In java 6, the introduction of the concept of a service provider, i.e., service can be configured in the configuration file (or interface may be an abstract class) of the provider (i.e., service implementation class). Configuration path: / META-INF / services / below. For more information, see: http: //docs.oracle.com/javase/6/docs/technotes/guides/jar/jar.html#Service%20Provider
and java.sql.DriverManager also added support for this, and therefore, in JDK6, the range of the DriverManager to find Driver:
Driver value 1) system property "jdbc.drivers" configured;
2) user calls the Class.forName () registered Driver
. 3) in-Service Provider profile java.sql.Driver Driver configuration values.
Therefore, in jdk6, in fact, you can not call Class.forName to load the mysql driver, because the driver mysql jar package already contains java.sql.Driver configuration file and add the com.mysql.jdbc in the file .Driver. but before JDK6 version, or to call this method.

 

参考文档:
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

 

Guess you like

Origin www.cnblogs.com/webglcn/p/11334799.html