When using JDBC, we all naturally have to use the following statements:
Copy Code code as follows:
Class.forName ("Com.mysql.jdbc.Driver");
String url = "Jdbc:mysql://127.0.0.1/test?useunicode=true&characterencoding=utf-8";
String user = "";
String PSW = "";
Connection con = drivermanager.getconnection (URL,USER,PSW);
Why it is very natural, because whether it is on the Internet or book tutorials on the example are such, and the program is really normal operation, so we also feel at ease to find gourd painting ladle down.
Do I have to have this? No, we can totally replace it with one of these words:
Copy Code code as follows:
Com.mysql.jdbc.Driver Driver = new Com.mysql.jdbc.Driver ();
Or
New Com.mysql.jdbc.Driver ();
String url = "Jdbc:mysql://127.0.0.1/test?useunicode=true&characterencoding=utf-8";
String user = "";
String PSW = "";
Connection con = drivermanager.getconnection (URL,USER,PSW);
As you may all see, we just need to make sure that the corresponding driver class has been loaded into the JVM before we call the DriverManager getconnection method, and that the initialization of the class is done. And how to achieve this function is not fastidious. The above two methods can implement this function, so the program can run normally. Note that if we do the following, the program is not functioning properly, because it only allows the driver class to be loaded into the JVM without the corresponding initialization.
Copy Code code as follows:
Com.mysql.jdbc.Driver Driver = null;
Or
ClassLoader cl = new ClassLoader ();
Cl.loadclass ("Com.mysql.jdbc.Driver");
We all know that JDBC is designed using bridge mode, DriverManager is one of the abstraction, Java.sql.Driver is a concrete implementation of the Implementor,com.mysql.jdbc.driver Implementor (refer to Gof's Bridge mode description). Note that the previous driver is an interface, but the latter is a class that implements the previous driver interface.
In bridge mode, Abstraction (DriverManager) is to have a implementor (Driver) reference, but in the course of use, we did not register the Driver object to the DriverManager. What the hell is going on here? The JDK documentation has this phrase in the driver description:
When a Driver class is loaded, it should create a instance of itself and register it with the DriverManager
Oh, it turns out that Com.mysql.jdbc.Driver helped us complete this step after loading. The source code is this:
Copy Code code as follows:
Package Com.mysql.jdbc
public class Driver extends Nonregisteringdriver implements Java.sql.Driver {
~ Static fields/initializers
Register ourselves with the DriverManager
//
static {
T ry {
Java.sql.DriverManager.registerDriver (New Driver ());
catch (SQLException E) {
throw new RuntimeException ("Can ' t Register driver!");
}
}
~ Constructors
/**
* Construct a new driver and register it with DriverManager
*
* @throws SQLException
* If a database error occurs.
*/
Public Driver () throws SQLException {
Required for Class.forName (). newinstance ()
}
}
PS: Modifying JDBC-driven mounts
Copy Code code as follows:
ClassLoader cl = Thread.CurrentThread (). Getcontextclassloader ();
Class clazz = Cl.loadclass ("Com.mysql.jdbc.Driver");
Clazz.newinstance ();
Connection conn = drivermanager.getconnection ("Jdbcurl");
can also be executed. However, a Com.mysql.jdbc.Driver instance is constructed in this way. With Class.forName ("Com.mysql.jdbc.Driver").
That
Copy Code code as follows:
Class.forName ("Com.mysql.jdbc.Driver") ==cl.loadclass ("Com.mysql.jdbc.Driver"). newinstance ();
Class.forName and Classloader.loadclass are different, one instantiation class, one load class