简体   繁体   中英

Access denied for user in mysql

I have followed all necessary steps while connecting my JAVA program to my database on mysql. my code snippet is as follows:

 final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
 final String DB_URL = "jdbc:mysql://localhost/electionDatabase/";
 final String User ="electionUser";
 final String Password="election";
 Class.forName(JDBC_DRIVER);
 conn = DriverManager.getConnection(DB_URL,"electionUser", Password);

 String sql = "select stateId from stateWiseSeats";

But still I am getting an error saying:

 Access denied for user 'electionUser'@'localhost' to database 'electionDatabase/'

i have rechecked my username and password on mysql commandline and also I granted added all the privileges to the user for the specified database. Still I am getting the error. Why?

As it says in the error message:

'electionDatabase/' 

is considered as the database name as per the URL provided and hence it is trying to access a non-existent database and throws this error.

Try removing the slash at the end of the URL and execute.

Class.forName("com.mysql.jdbc.Driver");
  // setup the connection with the DB.
  connect = DriverManager
      .getConnection("jdbc:mysql://localhost/electionDatabase?"
          + "user=electionUser&password=election");

This should solve your problem. Probably the trailing forward slash at the end of DB_URL is causing the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM