Preface
Awkward: access denied for user 'root'@'localhost' using password yes
Sometimes, when connecting MySQL data, an error message "access denied for user 'root'@'localhost' using password yes" will be reported. Don't be confused 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 situations, and we will talk about the solution.
1. Xinan MySQL has not set a password. In this case, you need to add a password. How to add it?
1. Use the root user to log in to MySQL with an empty password
mysql -u root
2. Modify the root user's password:
mysql>update database name set password=PASSWORD('new password') where USER='root'mysql>flush privileges;mysql>quit3. Restart MySQL and you can log in with your new password.
2. I haven't used it for a long time, forget my password
1. Open DOS and enter the bin directory under mysql: I: D:/Development/mysql-5.5.29-winx64/bin
2. Stop mysql service, net stop mysql
3. Enter after D:/Development/mysql-5.5.29-winx64/bin:
mysqld --defaults-file="D:/Development/mysql-5.5.29-winx64/bin/my.ini" --console --skip-grant-tables
4. Reopen a DOS window and enter after D:/Development/mysql-5.5.29-winx64/bin: mysql -root -p
5. Prompt to enter the password, enter the password after Enter, and enter mysql>
6. Enter under mysql>:
mysql>update database name set password=PASSWORD('new password') where USER='root'mysql>flush privileges;mysql>quitNote: If there are multiple databases, you can update them multiple times.
Configuring MySQL through properties files
1. Common ways to connect data
2. Properties configuration and reading
1. Configuration file users.properties
jdbc.drivers=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/databaseNamejdbc.username=rootjdbc.password=uppassword
2. Read the attribute file
(1) Create a Properties object;
Properties properties = new 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 in the Properties class to read the property list (key/value pair) from the input stream;
private String resource = "users.properties";//If the configuration file name is users.propertiesInputStream in = getClass().getResourceAsStream(resource);properties.load(in);
(3) When using database connection, use the getProperty() method in the Properties class to obtain the value value through the key, thereby realizing the database connection operation.
String drivers = props.getProperty("jdbc.drivers");String url = props.getProperty("jdbc.url");String username = props.getProperty("jdbc.username");String password = props.getProperty("jdbc.password");//Return an instance of the Connection class Class.forName(drivers);return DriverManager.getConnection(url, username, password);MySQL connection pool
1. Why use data sources and connection pools
Applications need to frequently connect to the database. If they connect to the database every time they operate and then close it, the performance will definitely be limited. Therefore, you must find a way to reuse the database connection. Use a connection pool to achieve the purpose of multiplexing database connections.
2. Connection pool concept
The connection pool is used to manage Connection objects. The connection pool can obtain connections from the data source. There can be several database connection objects in the connection pool, which can be reused. When an application needs to connect, it applies to the connection pool. If there are free connections in the connection pool, it will be assigned 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 the %CATALINA_HOME%/conf/server.xml file and add it under the <Host> node:
<!-- appName The project name docBase must be accurate and the symbol must be replaced with &--!><Context path="/appName" docBase="appName/WebRoot" auth="Container"> <Resource name="jdbc/MySQLDS" scope="Shareable" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/kqxt?useUnicode=true&characterEncoding=utf-8" driverClassName="com.mysql.jdbc.Driver" username="root" password="root" maxWait="3000" maxIdle="100" maxActive="10" /></Context>
3. Modify web.xml and add the following content under the <web-app> node
<resource-ref><description>Mysql Datasource example</description><res-ref-name>MySQLDS</res-ref-name><res-type>javax.sql.DataSource</res-type><res-auth>Container</res-auth></resource-ref>
4. Get database connection in code
// Pay attention to the imported package name import java.sql.Connection;import javax.naming.Context;import javax.naming.InitialContext;import javax.sql.DataSource;public class DBUtil {public static Connection getConnection() throws Exception { Context context = new InitialContext(); // Get the data source DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/MySQLDS"); // Get the database connection Connection conn = ds.getConnection(); if (conn != null && !conn.isClosed()) { return conn; } else { return null; } }}Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.