0

After installation of the mysql-connector-java-8.0.16.jar from the SQL database, despite my best efforts to get my code to work, I am left with an error of denied access for a user. This is peculiar because the same user credentials work on my site, so the user in question has the right permissions to access the database. What could be wrong. Am I using the wrong imports.

The imports in question:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

The code in question is:

package real;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class myConnection 
{
    public int returnOperation() 
    {
         try
            {
                String host = "jdbc:mysql://website.com/database";
                String username = "user";
                String password = "password";
                Connection con = DriverManager.getConnection(host, username, password);
            }
            catch(SQLException e)
            {
                System.out.println("There is an error with the SQL database: " + e.getMessage());
            }
        return 0;
    }
}

The error I keep receiving is:

There is an error with the SQL database: Access denied for user 'USER'@'IP ADDRESS' (using password: YES)
1
  • MySQL uses three pieces of information to authenticate a database user: username, password, and source IP. You have a username and password, but that user may not be authorised to connect from the IP you are connecting from. A GRANT ALL PRIVILEGES with @'%' for your user should allow that user to connect from any IP. Commented Jun 17, 2019 at 1:18

2 Answers 2

1

Good news looks like everything in your network is okay. Your error is specifically configuration.

Regarding the configuration, my first assumption is that you only allow local connections (from localhost).

Your problem is simply authentication. Try to connect with that user locally, if that succeeds, then you really need to tell mysql to allow connection with that user, from an IP or any IP to the server.

GRANT ALL PRIVILEGES ON database.* TO 'user'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

Or you can try the application from within the server and see if that succeeds.

Sign up to request clarification or add additional context in comments.

2 Comments

How would I do that in PhpMyAdmin where would I type this in sorry I'm new to MySql. Thanks!
If you have phpMyadmin you should be able to see the localhost server, and then all the tables under it. On the right pane you should see a tab that reads "SQL" and there you should be able to post what I shared with you previously. Don't forget to change the command to fit your needs.
1

As it's mentioned above

 GRANT ALL PRIVILEGES ON database.* TO 'user'@'%' WITH GRANT OPTION;
 FLUSH PRIVILEGES;

and since you're beginner, you can by going to sql tab in phpmyadmin

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.