Java MySql connection not functioning correctly

62 Views Asked by At

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)
2

There are 2 best solutions below

2
ddemuro On

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.

0
Anas Quedan On

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