Home  >  Article  >  Database  >  DriverManager connects to mysql database and is used in actual development

DriverManager connects to mysql database and is used in actual development

黄舟
黄舟Original
2017-08-10 15:27:561511browse

In actual development, DriverManager is used to connect to the mysql database


##1. The previous section introduced the use of Driver to connect to the database. , and written using the common method of Driver, but in actual development DriverManager is used as the database connection management.


##2. Create a new DriverManagerTest.java class in the project

DriverManager connects to mysql database and is used in actual development


3. Enter the following code

DriverManager connects to mysql database and is used in actual development


##4. Code explanation, the Drvier general method is still used here The writing method is to write the relevant information of the database in the jdbc.properties file.

The only difference is that the DriverManager driver only needs the following code when loading and connecting to the database.

This is much simpler than Driver.

DriverManager connects to mysql database and is used in actual development


##5. Test as follows in the main method , indicating that the connection is successful

DriverManager connects to mysql database and is used in actual development

##6. Question In Driver, we need to use reflection to create the implementation class Driver driver = (Driver) Class.forName(driverClass).newInstance(); and in DriverManager, we only need to do reflection to directly use its getConnection method, but there is no To actually register the driver: DriverManager.registerDriver(Class.forName(driverClass).newInstance());

##7. This requires viewing the source code of the Driver. Use the shortcut key ctrl+t on the Driver class to view its source code. Click Driver - com.mysql.jdbc

DriverManager connects to mysql database and is used in actual development

##8. Because the source code is in the src file, you will see the following picture


DriverManager connects to mysql database and is used in actual development

9. Click Attch Source to associate the source code


DriverManager connects to mysql database and is used in actual development

10. Select the SRC folder in the mysql file you downloaded


DriverManager connects to mysql database and is used in actual development

11. You will see the following static code. When the class It was already registered when it was initialized


The above is the detailed content of DriverManager connects to mysql database and is used in actual development. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn