theimpatientcoder
theimpatientcoder

Reputation: 1098

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?

Upvotes: 0

Views: 336

Answers (2)

Alan Francis
Alan Francis

Reputation: 1259

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.

Upvotes: 2

sp3tsnaz
sp3tsnaz

Reputation: 506

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.

Upvotes: 0

Related Questions