Why Class.forName ( "com.mysql.jdbc.Driver") can be loaded JDBC drivers

Class.forName()作用:

  1. Loading a class and its instantiated operation.
  2. The load used during the class loader is the current class.

com.mysql.jdbc.Driver Source:

// Driver Interface, all database vendors must implement, it indicates that this is a driver class

public class Driver implements java.sql.Driver {     
    public Driver() throws SQLException {     
    } 
    static {         
        try {           
            DriverManager.registerDriver(new Driver());  //注册数据库驱动         
        } catch (SQLException var1) {             
            throw new RuntimeException("Can't register driver!");         
        }     
    } 
} 
 

So the real registration drive is DriverManager, Class.forName is to tell the program the driver jar package which database to use.

Note: From JDBC3 beginning, it is now widely used version. Registration drive and can not be used directly. Class.forName sentence may be omitted.

Published an original article · won praise 0 · Views 71

Guess you like

Origin blog.csdn.net/qq_42606798/article/details/104102840