How to use MySQL for Java Web Learning in the project
preface
Awkward: access denied for user 'root' @'localhost 'using password yes
Sometimes, when connecting to MySQL data, an error message "access denied for user 'root' @'localhost 'using password yes" will be reported. Don't look silly when you see this error. The reason for the error is that the user name or password accessed by the database is incorrect. At this time, it is generally divided into the following two cases. Tell me the solutions respectively.
1、 Xin'an MySQL does not set a password. In this case, you need to add a password. How to add it?
1. Log in to MySQL as root with an empty password
2. To change the password of the root user:
3. Restart MySQL and you can log in with your new password
2、 I haven't used it for a long time. I forgot my password
1. Open dos and enter the bin directory under MySQL: myself: D: \ development \ mysql-5.5 29-winx64\bin
2. Stop MySQL service, net stop MySQL
3. In D: \ development \ mysql-5.5 Enter after 29 winx64 \ Bin:
4. Reopen a DOS window in D: \ development \ mysql-5.5 Input after 29 winx64 \ Bin: MySQL - root - P
5. Prompt for the password, enter the password after enter, and enter MySQL >
6. Under MySQL >, enter:
Note: if there are multiple databases, you can update multiple times.
Configure MySQL through the properties file
1、 Common ways to connect data
2、 Property file (. Properties) configuration and reading
1. Profile users properties
2. Read properties file
(1) Create an object of properties;
This step can also be done: create a class that inherits properties and get the object in singleton mode.
(2) Use the getresourceasstream() method of the class object to read the specified property file into the input stream, and use the load() method of the properties class to read the property list (key / value pair) from the input stream;
(3) When using the database connection, use the getproperty () method in the properties class to obtain the value value through the key, so as to realize the operation of the database connection.
MySQL connection pool
1、 Why use data sources and connection pools
Applications need to connect to the database frequently. If you connect to the database and then close it every time, the performance will be limited. Therefore, we must find a way to reuse the connection of the database. Using connection pool can achieve the purpose of reusing database connections.
2、 Connection pool concept
The connection pool is used to manage connection objects. The connection pool can obtain connections from data sources. There can be several database connection objects in the connection pool, which can be reused. When an application needs a connection, it applies to the connection pool. If there is an idle connection in the connection pool, it will be allocated to the application. If not, it may need to wait in the waiting queue.
3、 MySQL connection pool configuration
1. Copy the database driver package and JSTL jar package to% Catalina_ Home% \ lib.
2. Modify% Catalina_ HOME%\conf\server. XML file, add the following under the < host > node:
3. Modify web XML, add the following content under the < web app > node
4. Get database connection in code
summary
The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.