The role of class.forname in JDBC _java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.