[Solved] java.sql.SQLException: Access denied for user ‘[email protected]’@’localhost’ (using password: YES)

I am connecting Java using Eclipse with MySQL db


import java.sql.*;

public class DbDemo {

    public static void main(String args[]) throws ClassNotFoundException, SQLException {

        String s;       
        String uname="[email protected]";
        String url="jdbc:mysql://localhost:3306/student";

        String password="Hsun123";

        int i;

        try {


            Connection con=DriverManager.getConnection(url,uname,password);

            Statement st=con.createStatement();

            ResultSet rs=st.executeQuery("select * from student_detail");

            if( {







        } catch(Exception e) {


java.sql.SQLException: Access denied for user '[email protected]'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(
at com.mysql.jdbc.MysqlIO.checkErrorPacket(
at com.mysql.jdbc.MysqlIO.checkErrorPacket(
at com.mysql.jdbc.MysqlIO.checkErrorPacket(
at com.mysql.jdbc.MysqlIO.secureAuth411(
at com.mysql.jdbc.MysqlIO.doHandshake(
at com.mysql.jdbc.ConnectionImpl.coreConnect(
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(
at com.mysql.jdbc.ConnectionImpl.createNewIO(
at com.mysql.jdbc.ConnectionImpl.<init>(
at com.mysql.jdbc.JDBC4Connection.<init>(
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(
at com.mysql.jdbc.ConnectionImpl.getInstance(
at com.mysql.jdbc.NonRegisteringDriver.connect(
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at database.DbDemo.main(

What should I do to resolve my problem?

Solution #1:

Below queries, execution has fixed my problem:

sudo mysql

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';


Respondent: Rajaneesh

Solution #2:

Instead of using :

 String uname="[email protected]";

Use :

String url="jdbc:mysql://localhost:3306/student";
String userName="root"
String password="Hsun123"


        Connection con=DriverManager.getConnection(url,username,password);

This should work (provided you are setting the valid password)

Respondent: Amitesh Rai

Solution #3:

If you are connecting a remote mysql server from your local machine using java see the below steps.

Error : java.sql.SQLException: Access denied for user ‘xxxxx’@’’ (using password: YES)

for remote access may be cPanel or others grant the remote access for your local ip address.

In the above error message: “” is the local machine IP. So First we have to give remote access in the cPanel-> Remote MySQL®. Then run your application to connect.

Connection con=DriverManager.getConnection(  
//here abc is database name, def is username and ghi        
Statement stmt=con.createStatement();  
ResultSet rs=stmt.executeQuery("select * from employee");  
        System.out.println(rs.getInt(1)+" "+rs.getString(2)+"  

Hope it will resolve your issue.


Respondent: S Debasish Nayak

Solution #4:

Well, same problem here. I’m using phpmyadmin.

I had to go to phpmyadmin->users->root->Edit Privileges.

Then on the field “Database-specific privileges” I had to provide privileges for my database.

On the field “Change password” I retyped my password (I don’t know why I had to do that, maybe a possible bug since i already had one).

On the field “Login Information->Host” I put value Any host.

Problem is solved.

Respondent: George Tsamis

Solution #5:

try this…

String password="Hsun123"; 

instead of

String password=""; 
Respondent: kazi

Solution #6:

Make sure you have a privileged (i.e. neccessary rights) user account with correct password exist in your database. or just creat no password at all

Respondent: henry.w

Solution #7:

For those who are suffering from this problem

java.sql.SQLException: Access denied for user ‘root’@’localhost’

(using password: YES)

Solution for that is:

Whatever password you are providing on this line in your code

Connection con=DriverManager.getConnection( 

No need to provide this password just insert ""

Complete Code is:

import java.sql.*;  
class TestDB{  
    public static void main(String args[]){  
        Connection con=DriverManager.getConnection(         "jdbc:mysql://localhost:3306/Pravin","root","");  
        //here sonoo is database name, root is username and password  
        Statement stmt=con.createStatement();  
        ResultSet rs=stmt.executeQuery("select * from student");  
            System.out.println(rs.getInt(1)+"  "+rs.getString(2)+" "+rs.getString(3));  
    }catch(Exception e){
Respondent: Pravin Garodi

Solution #8:

My answer with the new connector for a data base myPhpAdmin :

spring.datasource.url= jdbc:mysql://localhost:8889/mydb?serverTimezone=UTC
spring.datasource.username= root
spring.datasource.password= root
spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver false
spring.jpa.hibernate.ddl-auto= update
#spring.jpa.hibernate.ddl-auto= create org.hibernate.dialect.MySQL5Dialect
spring.main.banner-mode= off
Respondent: Laura Rotellini

Solution #9:

Late answer, but findings can help others 🙂 Faced similar issue, i was trying to connect database from Spring boot application developed using Eclipse. Open Mysql Command Prompt or GUI tool and run following query.

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '%your_password%' WITH GRANT OPTION;

Also check your TCP/IP service for database are enabled or not.

Respondent: Joginder Malik

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy