How to write a common file connecting to the mysql database in eclipse
## Recommended related mysql video tutorials: "mysql tutorial"
1. The previous example is to write the database driver, connection, user name and password in the class. The coupling is too high. When our database changes or the database type is changed, we need to change the code again, which is very inconvenient.
Solution: Write the database driver, connection, user name and password in the configuration file, write the code by reading the configuration file, and modify it directly if the database changes in the future Just configure the file!
2. Right-click on the project to create a new file and name it jdbc.properties
##3. The creation is completed as shown in the figure:
4. Enter the following in the jdbc.properties file Information, respectively, are the database driver, connection, user name and password
##5. Create a new JdbcTest2.java class
## 6. Enter the following code:
7. Code description:
This paragraph The code is to read the configuration file and read out each item in the configuration file by name
8. This paragraph The code creates the Driver object through reflection, which is the instantiation of the class
9. Enter the following in the main function , test method
10. The result after running is as follows, indicating that the connection is successful!
The above is the detailed content of How to write a common file connecting to MySQL database in eclipse. For more information, please follow other related articles on the PHP Chinese website!