JDBC Learning 2: Why Write Class.forName ("XXX")?

Source: Internet
Author: User

Class.forName (String name)

Connect to the previous JDBC. Originally this content is put in front of an article inside of together, later found the more write more, think to see on the forget, or separate open an article good, so also can write more detailed point.

In the 4th, getconnection () method of the previous article, I replaced the mysqlpackage from the. Properties with the actual value, so the replacement should be Class.forName (" Com.mysql.jdbc.Driver "), in fact all JDBC connections are basically written in this sentence. In addition, even if you delete this sentence, it can be run successfully. Then why do you have to Class.forName ("Com.mysql.jdbc.Driver")? OK, a bit of explanation.

1, why should Class.forName ("Com.mysql.jdbc.Driver")?

JDBC is a typical application of bridge mode in 23 modes, familiar with bridging mode basically a little bit to see the source code to know why, that bridge mode here does not speak, only to explain why to do so. Class.forName (String className) has two functions, the first is a. class file with the specified name under Classpath loaded into the Java Virtual Machine memory, and the second is initializing the class. See this sentence, the return value is not, the role of writing here is very obvious, is to initialize the "Com.mysql.jdbc.Drvier". What does initialization do? Assign values to static resources and execute static blocks of code, so decompile the jar package "Mysql-connector-java-5.1.20-bin.jar" and look at the driver class:

public class Driver extends Nonregisteringdriver  implements java.sql.driver{public  Driver ()    throws SQLException  {  }  static  {    try    {      drivermanager.registerdriver (new Driver ());    } catch (SQLException E) {      throw new RuntimeException ("Can ' t Register driver!");}}  

Seeing the role of Class.forName ("Com.mysql.jdbc.Driver") is actually calling DriverManager's Registerdriver method to register a MySQL JDBC driver (Driver). The driver inheritance Nonregisteringdriver.java,nonregisteringdriver.java implements the driver interface provided by the JDK, which provides several ways to connect to the database, Each of the different database connection classes must implement it and override the algorithm for the specific database connection. DriverManager is also a class in the JDK that truncates some key code:

New copyonwritearraylist ();
void Registerdriver (Driver paramdriver)    throwsnull) registereddrivers.addifabsent (new new nullpointerexception (); println ("Registerdriver:" + paramdriver);}     

The bottom layer uses a copyonwritearraylist as a container (this is a thread-safe container, but every time the add has a new copy of the underlying array, it is recommended to use this when reading far more than writing), and to put the registered Driverinfo. Final getconnection (...) When you take registerdrivers inside the specific database driverinfo (like the MySQL driver in driverinfo) to connect to the specific database. OK, so summarize the whole process:

JDK is not responsible for dealing with the database connection, there is no need, only to provide a specific interface driver, tell all the third party, to connect to the database, to implement this interface, and then register through DriverManager, when the time to connect a database, you have registered with me, I will call you to register in the driver inside the method to connect to the specified database. Then MySQL realizes its own driver,oracle realization own driver, registers through the static block, then, then, then does not have.

2, why not directly new?

It means "com.mysql.jdbc.Driver d = new Com.mysql.jdbc.Driver ();", yes, because the initialization of a class is automatically triggered when new. The question is, what's new doing here? Are we going to use the method inside the com.mysql.jdbc.Driver, and do we know how to use it? It is better not to go to the new class simply to initialize a class but to initialize it directly with Class.forName (String name). DriverManager class of getconnection (...) The existence of the method itself is to help users call driver inside the various methods to connect the database, the JDK is done, developers do not need to write their own.

3, why delete class.forname ("Com.mysql.jdbc.Driver") or can run?

January 23, 1996 JDK1.0 released, the Java language has the first official version of the operating environment. JDBC was released in the JDK1.1 version of February 19, 1997, and from the version it is seen that JDBC belongs to some of the most basic functional points of Java technology. After the JDK1.5, in fact there is no need to explicitly call Class.forName ("Com.mysql.jdbc.Driver"), DriverManager will automatically load the appropriate driver, but the premise is The drive jar package must be available under Classpath .

JDBC Learning 2: Why Write Class.forName ("XXX")?

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.