Troubleshooting "java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)"
The error "java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)" occurs when attempting to connect to a MySQL database using the root user with the provided credentials.
Cause:
This error is caused due to incorrect credentials or insufficient privileges for the root user on the specified host.
Solution:
To resolve this issue, you can try the following:
<code class="sql">GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '%password%' WITH GRANT OPTION;</code>
Replace %password% with the actual password you want to use.
<code class="sql">FLUSH PRIVILEGES;</code>
<code class="java">Class.forName("com.mysql.jdbc.Driver"); Connection m_connection = DriverManager.getConnection("jdbc:mysql://localhost/?user=root&password=rootpassword&database=databasename");</code>
By following these steps, you should be able to establish a successful connection to the MySQL database as the root user.
The above is the detailed content of Here are a few title options, incorporating the question-and-answer format: * **Why Am I Getting \'java.sql.SQLException: Access denied for user \'root\'@\'localhost\' (using password: YES)\&quo. For more information, please follow other related articles on the PHP Chinese website!