JDBC Detailed series (ii) load driver

Source: Internet
Author: User

---[From my csdn blog] (http://blog.csdn.net/weixin_37139197/article/details/78838091)---

?? In the JDBC Detail Series (i) process, I decomposed the database connection into six steps.

JDBC Process:
The first step: Load the driver class, register the database driver;
The second step: through the DriverManager, using the URL, user name and password to establish a connection (Connection);
Step three: Open the Statement object by connection, using SQL statement;
Fourth step: Execute the statement, return the result to resultset;
The fifth step: the result resultset processing;
Sixth step: Flashback release Resources resultset-"preparedstatement-" connection.

?? Then the first step of loading the database driver is explained. The load driver is the code of this sentence:

public static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";Class.forName(JDBC_DRIVER);//以上也可以直接替换为 new com.mysql.jdbc.Driver();

?? Class.forName (String className) returns a class that, in this process, loads the class into the JVM, where the static code of the class executes. Why not use new Com.mysql.jdbc.Driver () this way?

If you use new Com.mysql.jdbc.Driver () This way, you will have a dependency on this particular class. Later if you want to replace the database driver, you will have to re-modify the code. With reflection, you only need to change the driver and URL in the configuration file.

?? What happened during the load-driven process? Let's take a look at the MySQL driver class code.

public class Driver extends NonRegisteringDriver implements java.sql.Driver {    //    // Register ourselves with the DriverManager    //    static {        try {            java.sql.DriverManager.registerDriver(new Driver());        } catch (SQLException E) {            throw new RuntimeException("Can‘t register driver!");        }    }    /**     * 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()    }

?? OK, which is actually the code that works:

 java.sql.DriverManager.registerDriver(new Driver());

?? Look at the source of the registerdriver, basically if there is no such class in DriverManager, the class is added to the list of DriverManager. In the actual use of the process, we can completely use the code above to load the driver code is replaced, but also to the specific class dependencies, resulting in subsequent changes inconvenience.

Class.forName(JDBC_DRIVER);

?? To this, I want to test the load driver code to remove, and then see what errors will be reported. It turned out to be no problem. Is this teasing me? That's not how it's taught in the book. Google only, for the following reasons:

New features are added after JDBC4.0: JDBC4.0 no longer needs to display the call Class.forName () registration driver, DriverManager initialization through the Serviceloader class, Find in our classpath jar (Database driver package), use the class name in the Meta-inf\services\java.sql.driver text to register.

?? Also say that, at the time of launching, through the jar package under the Java.sql.Driver text content, help you to load the driver.
?? OK, after understanding this, I deleted the text content in the java.sql.Driver in the MySQL driver jar package, and then commented out the Class.forName (Jdbc_driver), which is an error:

Java.sql.SQLException:No suitable driver found for jdbc:mysql://localhost:3306/jdbc_test

??

---[From my csdn blog] (http://blog.csdn.net/weixin_37139197/article/details/78838091)---

JDBC Detailed series (ii) load driver

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.