1

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
  • 1
    Yes it's connecting, but it's not logging in because the credentials are wrong. Commented Jan 21, 2014 at 17:16

5 Answers 5

7

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.

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

1 Comment

MySQL said: Documentation #1044 - Access denied for user 'BOB'@'%' to database 'TEST'
0

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.

Comments

-1

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 Comments

Downvoted because this does not answer the question.
I am using a godaddy database, the username and password are the ones I supplied when creating it.
Did you put the username in the database and give it privileges? Like this: website-in-a-weekend.net/wp-content/uploads/2010/03/…
There is no option for creating users for the database. Just the log in credentials?
I don't know how to give user privileges without adding it to the database (I've never used godaddy database). Please look at waltzZiss's suggestion, I think he's correct.
-1

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 Comment

create a new user with all privileges and connect it with netbeans
-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 Comment

I am not getting proper answer when I'm run then com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user ''@'localhost' to database '/test' show and not uodate in database

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.