Java Database mySQL Access Denied

Java Database mySQL Access Denied

So I am trying to connect to my database and display an item from the table. The error I am getting is: SQL Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user 'Bob'@'%' to database 'TEST'

Is this connecting properly, and if so is the error that the credentials are wrong? And if they are wrong how is it connecting? Thank you

try 
     {  
        Class.forName("com.mysql.jdbc.Driver");        
        String url = "jdbc:mysql://THISISTHEHOSTNAME";
        String username = "Bob";
        String password = "password";
        Connection connection = DriverManager.getConnection(url, username, password);
         Statement stmt = null;
        ResultSet rs = null;
        //SQL query command
        String SQL = "SELECT * FROM TEST";
        stmt = connection.createStatement();
        rs = stmt.executeQuery(SQL);
        while (rs.next())
        {
            System.out.println(rs.getString("ProductName") + " : " + rs.getString("UnitPrice"));
        }
     } 
     catch (SQLException e) 
     {
        System.out.println("SQL Exception: "+ e.toString());
     } 
     catch (ClassNotFoundException cE) 
     {
        System.out.println("Class Not Found Exception: "+ cE.toString());
     }
1

5 Answers

You need to grant the proper privileges to the user that is connecting to the mysql db.

The message you are getting is informing you that while your user was able to connect to the database server, it was not allowed to access the database TEST.

Running the following command in the mysql console would grant such access:

GRANT ALL ON TEST.* TO 'BOB'@'%'

This is extremely permissive and you should keep in mind that db users should have the minimal amount of privileges possible and be restricted to the smallest range of hosts.

1

I used ShowIP Firefox add on and used the IP instead and didn't get any errors but I'm wondering if should return access granted and I'm unable to find an answer at the moment.

Here's a few things to do:

  1. Check to see if your username and password are correct.
  2. Did you add the username to the correct database? (This needs to be done in CPanel SQL)
  3. Did you allow your database to get connection access from your IP address? (This also needs to be done in CPanel SQL)
5

create a new user in mysql check all global privileges > go to service in netbeans > database > mysql server at localhost > right click connect and fill username password

this work for me

1

Please try the code below:

import java.sql.*;
public class InsertPrepared {

   public static void main(String[] args)
   {
      try
      {
         Class.forName("com.mysql.jdbc.Driver");
         Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306//test","sanjib","");
         PreparedStatement stmt=con.prepareStatement("insert into employee values(???)");

         stmt.setInt(1,101);
         stmt.setString(2,"Sampa");

         int i=stmt.executeUpdate();
         System.out.println(i+"Records is inserted");
         con.close();
      }

      catch(Exception e)
      {
         System.out.println(e);
      }

   }
}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.