When trying to establish a connection to a MySQL database using the Java code snippet below:
<code class="java">Class.forName("com.mysql.jdbc.Driver"); Connection m_connection = DriverManager.getConnection("jdbc:mysql://localhost","root","root");</code>
you may encounter the following SQLException:
<code class="java">java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)</code>
This error is typically caused by a lack of proper permissions for the root user. To resolve this issue:
<code class="sql">GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '%password%' WITH GRANT OPTION;</code>
Once the permissions are granted, you should be able to connect to the database successfully using the specified credentials.
The above is the detailed content of How to Fix \'Access denied for user \'root\'@\'localhost\'\' Error when Connecting to MySQL with Java?. For more information, please follow other related articles on the PHP Chinese website!