0

I am trying to connect to mysql database from my java application. I am using Connector J driver. I can connect to mysql from 'cmd' and netbeans but when I try to connect to the database using java code it shows error 1045 sqlstate 28000 Access denied for user 'sayeed'@'localhost'. I've searched various topics like: Access denied for user 'root'@'localhost' (using password: YES) (Mysql::Error) and How to connect NetBeans to MySQL database? but none of them worked for me.

try {
    Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
    System.out.print("Driver not Found");
}

try {
    connect = DriverManager.getConnection(
            "jdbc:mysql://localhost:3306/sqltest", "sayeed", "miracle");
    System.out.printf("okay");
} catch (SQLException e) {
    System.out.print(e.getSQLState() + " " + e.getErrorCode());
    System.out.print("\n" + e.getMessage() + "\n");
}

This shows the error:

28000 1045
Access denied for user 'sayeed'@'localhost' (using password: YES)

using 'cmd' i can connect to mysql accessing database from cmd and i can connect to the database using netbeans new connection wizard. connecting to database from netbeans

Note: I have granted all privileges to 'sayeed' my user table in mysql database shows (all privileges for sayeed)

Can anyone tell me what i'm doing wrong?

Community
  • 1
  • 1
sayeed910
  • 198
  • 1
  • 9
  • You'll be better off if you print the stack trace in your catch blocks rather than selectively choosing messages. Look at this question and scroll down to the 3rd answer by @Doug: http://stackoverflow.com/questions/16325115/sqlstate28000-1045-access-denied-for-user-rootlocalhost-using-password – duffymo Apr 22 '16 at 12:17
  • my root password is blank – sayeed910 Apr 22 '16 at 12:29
  • You aren't using the root password to log in via Java (and you shouldn't under any circumstance). I see username "sayeed" and password "miracle". That's a different user from root. Did you GRANT that user access? sayeed != root. – duffymo Apr 22 '16 at 12:31
  • @duffymo, yes i have granted all privileges for database sqltest to sayeed.In fact, 'sayeed' has all the privileges 'root' has (global and for 'sqltest'). – sayeed910 Apr 22 '16 at 13:01
  • @duffymo, sayeed@localhost – sayeed910 Apr 22 '16 at 13:15

0 Answers0