Class.forName()作用:
- Loading a class and its instantiated operation.
- 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.