Thursday, October 13, 2011

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

if you get below mentioned exceptions while connecting My SQL using JDBC, Just make sure that password and other login credentials are provided correctly.

Exception in thread "main" java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
        at com.mysql.jdbc.SQLError.createSQLException(
        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.createNewIO(
        at com.mysql.jdbc.ConnectionImpl.(
        at com.mysql.jdbc.JDBC4Connection.(
        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)

Solution :

Just make sure that password you provide is correct, Cross check the password you have provided.

Below is the simple JDBC test program, To run this program make sure you add Jar file like mysql-connector-java-5.1.5.jar to classpath.

Make sure you provide all correct login credentials in Step 2. i.e. port, database name and password.
/* */

import java.sql.*;
public class Test
public static void main(String[] args) throws Exception
Connection con;
Statement stat;
Class.forName("com.mysql.jdbc.Driver");//Step 1: Loading Drivers

con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Test","root","password");//Step 2: Making Connection

stat=con.createStatement();//Step 3: Creating JDBC Statement

String query = "select * from table_name;";

ResultSet rset=stat.executeQuery(query);//Step 4: Execute the Ststement

while( 5: Looping through the ResultSet
stat.close();//step 6: Close the Connection and Statement


  1. fantastic points altogether, you just won a brand new reader.
    What would you suggest in regards to your publish that you simply made some days in the past?
    Any positive?

    Also visit my page :: twitter password reset

  2. Thanks , I've just been searching for info about this topic for ages and yours is the best I have came upon so far. But, what about the conclusion? Are you positive about the source?

    Also visit my web site; MineCraft Premium Generator

  3. Hey There. I found your blog using msn. This
    is a very well written article. I'll be sure to bookmark it and return to read more of your useful info. Thanks for the post. I'll definitely comeback.

    Also visit my web site :: reset password

  4. Hey There. I discovered your weblog the usage of msn.
    That is a really smartly written article. I'll make sure to bookmark it and come back to read more of your useful information. Thanks for the post. I will certainly return.

    Also visit my weblog how to get free cash

  5. Hello, i feel that i noticed you visited my web site so i came
    to return the desire?.I'm attempting to find things to enhance my web site!I suppose its adequate to make use of some of your ideas!!

    my web site: hack twitter

  6. Really when someone doesn't know afterward its up to other visitors that they will assist, so here it occurs.

    My web blog; Minecraft accounts

  7. I'm not sure why but this web site is loading incredibly slow for me. Is anyone else having this issue or is it a issue on my end? I'll check back later and see if the problem still exists.

    Feel free to visit my web blog ... Windows 7 Password Cracking

  8. If you wish for to obtain a great deal from this post then
    you have to apply such methods to your won webpage.

    Review my page: real estate marin County ca

  9. This program is intended to recover lost passwords for RAR/WinRAR
    archives of versions 2.xx and 3.xx. http://www.passwordrecoveryforrar.

    tk The free professional solution for recovering lost passwords to RAR and WinRAR

    I do not even know how I stopped up right here, but I thought this publish
    used to be good. I don't recognize who you are but certainly you are going to a well-known blogger in the event you aren't already.