Error: Access denied for user ‘root’ @ ‘localhost’ using password yes
Error: Access denied for user ‘root’ @ ‘localhost’ using password yes The reason for the error is that the user name or password for database access is incorrect. At this time, it is generally divided into the following two situations, and the solution is described respectively.
MySQL > update MySQL > update MySQL > update MySQL > update MySQL > update MySQL > update MySQL > update MySQL
1. Log in to the MySQL database as user root with an empty password
mysql -u root
Copy the code
Change the password of user root.
Mysql >update Database nameset password=PASSWORD('New password') where USER='root'
mysql>flush privileges;
mysql>quit
Copy the code
3, restart MySQL, you can use the new password to log in
Two, long time no need, forget the password
1, open DOS and enter mysql bin directory: D: Development\mysql-5.5.29-winx64\bin 2, net stop mysql 3, 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 D:\Development\mysql-5.5.29-winx64\bin: mysql -root -p
Mysql >update Database nameset password=PASSWORD('New password') where USER='root'
mysql>flush privileges;
mysql>quit
Copy the code
Note: Multiple update operations can be performed if there are multiple databases.
Configure MySQL through properties files
1. Common ways to connect data
- Encoding mode in which database configuration information is written directly into JAVA code
- Properties property file, which writes database configuration information to the Properties file and then reads the Properties file in the program.
- DataSource, which uses JNDI to get the DataSource object to the Connection object.
- Hibernate configuration
- The Spring configuration
Properties file configuration and reading
1. Configure the file users.properties
jdbc.drivers=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/databaseName
jdbc.username=root
jdbc.password=upassword
Copy the code
2. Read the properties file
(1) Create a Properties object;
Properties properties = new Properties();
Copy the code
This step can also be done by creating classes that inherit from Properties and get objects 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 pairs) from the input stream;
private String resource = "users.properties"; // If the configuration file name is users.properties InputStreamin = getClass().getResourceAsStream(resource);
properties.load(in);
Copy the code
(3) When using database connection, use getProperty() method in Properties class to obtain value through key, so as to realize 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.forname (drivers);return DriverManager.getConnection(url, username, password);
Copy the code
MySQL connection pool
Why use data sources and connection pools
The application needs to connect to the database frequently, and if you connect to the database for every operation and then close it, performance will be limited. So, find a way to reuse your database connections. You can reuse database connections using connection pooling.
2. Connection pool concept
Connection pooling is used to manage Connection objects, which can obtain connections from data sources, and can have several database Connection objects that can be reused. When an application needs a connection, it requests it from the connection pool, and if there are free connections in the pool, they are allocated to the application; if not, it may need to wait in a wait queue.
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 and add:
<! -- appName is the name of the project docBase must be accurate and ampersand is replaced with ampersand --! > <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>
Copy the code
3. Modify web. XML and add the following content under the 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>
Copy the code
4. Get the database connection in code
// 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(); DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/MySQLDS"); Conn = ds.getConnection();if(conn ! = null && ! conn.isClosed()) {return conn;
} else {
returnnull; }}}Copy the code