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.
##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.
##5. Test as follows in the main method , indicating that the connection is successful
##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
##8. Because the source code is in the src file, you will see the following picture
9. Click Attch Source to associate the source code
10. Select the SRC folder in the mysql file you downloaded
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!