Home > Database > Mysql Tutorial > Introduction to three methods of obtaining mysql connection in Java (picture)

Introduction to three methods of obtaining mysql connection in Java (picture)

黄舟
Release: 2017-08-20 14:27:55
Original
1439 people have browsed it

Recently I need to use mysql connection at work, and I found that there is more than one way to achieve it, so I will summarize it. The following article mainly introduces you to 3 methods on how to obtain mysql connection in Java. Friends who need it can For reference, friends who are interested can follow the editor to learn together.

Preface

This article mainly talks about three ways to obtain mysql connection in Java, and share them for your reference and study. The following is not Enough said, let’s take a look at the detailed introduction:

  • The first one: traditional connection method;

  • The second one: read Take the configuration file method;

  • The third method: database connection pool.

1. Traditional connection method:

First create a folder named lib in the src directory. Import the database driver jar package and then "add to build path".

1. Register the driver


 Class.forName("com.mysql.jdbc.Driver");
Copy after login

2. Get the connection


 String url = "jdbc:mysql://localhost:3306/shun";
 String user = "root";
 String password = "";
 Connection conn = DriverManager.getConnection(url, user, password);
 Finish.
Copy after login

Codes:

##2. How to read the configuration file

First we create a .properties configuration file in the src directory, with the following content:


1. Read the configuration file


InputStream is =  Demo02.class.getClassLoader().getResourceAsStream("db.properties");
Copy after login

2. Create a Properties object


 Properties pro = new Properties();
Copy after login

3 .Load


 pro.load(is);
Copy after login

4.Parse


 String driver = pro.getProperty("driver");

 String url = pro.getProperty("url");

 String user = pro.getProperty("user");

 String password = pro.getProperty("password");
Copy after login

5 .Register the driver to get the connection


 Class.forName(driver);

 Connection conn = DriverManager.getConnection(url, user, password);

 Finish.
Copy after login

Codes:


3. Database connection pool

Similarly, we first create a folder named lib in the src directory, import the jar package of DBUtils and database connection pool, and then "add to build path".

1. Create a basic data source


 BasicDataSource bds = new BasicDataSource();
Copy after login

2. Set properties


 bds.setDriverClassName("com.mysql.jdbc.Driver");

 bds.setUrl("jdbc:mysql://localhost:3306/shun");

 bds.setUsername("root");

 bds.setPassword("");

 Finish.
Copy after login

#Codes:


##Conclusion:

Through the above analysis of the three Java Get an introduction to the mysql connection method. I believe you have already had a preliminary understanding of this. The first method is obviously a hard compilation method, which is to hard-code all the data. It will be very troublesome to change in subsequent development; and the second method is hard compilation. The second method is slightly better than the first method, because next time we use it, we can directly modify the .properties configuration file; and for the third method, we use the DBCPUtils package that has been written for us, directly By omitting the Connection object, the execution is simpler, the code is more concise, the resource utilization rate is lower, and it is easier to modify when the time comes. Therefore, the method of using the database connection pool is relatively better.

Summarize

The above is the detailed content of Introduction to three methods of obtaining mysql connection in Java (picture). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template