Home >Database >Mysql Tutorial >Why Does My Java Code Throw an 'Access Denied' Error When Connecting to MySQL?

Why Does My Java Code Throw an 'Access Denied' Error When Connecting to MySQL?

Susan Sarandon
Susan SarandonOriginal
2024-11-29 14:58:10555browse

Why Does My Java Code Throw an

"Fixing Exception: java.sql.SQLException: Access Denied for User with Empty Credentials"

Question:

An exception with the message "java.sql.SQLException: Access denied for user ''@'localhost' (using password: NO)" is encountered while trying to connect to a MySQL database. All attempts to change the user's password, grant permissions, or reconnect have failed. What is the root cause of this error?

Answer:

The provided Java code contains a discrepancy in the main method call:

public void main(String[] args) throws SQLException

This method is likely part of a different class, "com.mysql.cj.jdbc.admin.TimezoneDump," which also defines a main method. As a result, Eclipse may be running this incorrect class instead of the intended "MainApp" class.

To resolve this issue, you can manually指定 which class to run:

  1. Right-click on MainApp.java.
  2. Select Run As.
  3. Choose Java Application.

This action will instruct Eclipse to execute the "MainApp" class, which should successfully connect to the database without the access denied error.

The above is the detailed content of Why Does My Java Code Throw an 'Access Denied' Error When Connecting to MySQL?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn