Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access denied for user 'root'@'localhost' (using password: YES): PhpMyAdmin

Given this code :

package db;

import java.sql.*;

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

        Class.forName("com.mysql.jdbc.Driver");

        String string = "jdbc:mysql://localhost:3306/testdb";

        Connection con = DriverManager.getConnection(string , "root" , "root");
        PreparedStatement statement = con.prepareStatement("select * from name");
        ResultSet result = statement.executeQuery();
        while (result.next())
        {
            System.out.println(result.getString(1) + " " + result.getString(2));
        }


    }
}

I'm trying to connect mysql server but I get this :

Exception in thread "main" java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1694)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1244)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2397)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2430)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2215)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:813)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    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(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at db.Main.main(Main.java:14)

Line 14 is that : Connection con = DriverManager.getConnection(string , "root" , "root");

I'm sure that when I installed MySql I entered the password root . Just to make sure , I entered here : to this ,

and changed the password to root , like this : enter image description here , but I still can't get any correct output .

Any idea what's wrong with it ?

Thanks

REMARK: Just to be clear , I'm using :

  1. Eclipse Java EE IDE for Web Developers.

  2. Tomcat 7

  3. Xampp

like image 384
JAN Avatar asked Nov 24 '25 02:11

JAN


1 Answers

Exception in thread "main" java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

You get this error when user has no rights to access localhost.

You need to grant access to root from localhost.

As you have added that. I doubt that problem lies with the row with password='NO' with mySQL settings.

like image 180
Hardik Mishra Avatar answered Nov 26 '25 18:11

Hardik Mishra