Home  >  Article  >  Database  >  How to write a common file connecting to MySQL database in eclipse

How to write a common file connecting to MySQL database in eclipse

黄舟
黄舟Original
2017-08-04 14:18:502531browse

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

How to write a common file connecting to MySQL database in eclipse

##3. The creation is completed as shown in the figure:

How to write a common file connecting to MySQL database in eclipse

4. Enter the following in the jdbc.properties file Information, respectively, are the database driver, connection, user name and password

How to write a common file connecting to MySQL database in eclipse##5. Create a new JdbcTest2.java class

How to write a common file connecting to MySQL database in eclipse


## 6. Enter the following code:

How to write a common file connecting to MySQL database in eclipse

7. Code description:

This paragraph The code is to read the configuration file and read out each item in the configuration file by name

How to write a common file connecting to MySQL database in eclipse8. This paragraph The code creates the Driver object through reflection, which is the instantiation of the class

How to write a common file connecting to MySQL database in eclipse9. Enter the following in the main function , test method

How to write a common file connecting to MySQL database in eclipse10. The result after running is as follows, indicating that the connection is successful!

How to write a common file connecting to MySQL database in eclipse

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!

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