7

I am connecting Java using Eclipse with MySQL db

CODE

import java.sql.*;
import java.io.*;

public class DbDemo {

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

        String s;       
        String uname="root@localhost";
        String url="jdbc:mysql://localhost:3306/student";

        String password="Hsun123";

        int i;

        try {

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

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

            Statement st=con.createStatement();

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

            if(rs.next()) {

                i=rs.getInt(1);

                s=rs.getString(2);

                System.out.println(i+"/t"+s);
            }           

            rs.close();

            st.close();

            con.close();

        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

ERROR

java.sql.SQLException: Access denied for user 'root@localhost'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:935)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:4101)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1300)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2337)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2370)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2154)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:792)
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:381)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at database.DbDemo.main(DbDemo.java:13)

What should I do to resolve my problem?

2
  • is the password correct for root@localhost? Commented Jul 19, 2014 at 8:22
  • 3
    I'd try String uname="root"; Commented Jul 19, 2014 at 8:22

10 Answers 10

2

Instead of using :

 String uname="root@localhost";

Use :

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

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

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

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

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

1 Comment

Can someone delete the 10 m's that I added. There was a 4 character change I made usernamename -> username that didn't meet the 6 character limit. So I thought by adding extra characters, and then deleting them in a separate edit would be a clever way around the rule. Well it wasn't. Sorry about that.
2

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'@'101.123.163.141' (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: "101.123.163.141" is the local machine IP. So First we have to give remote access in the cPanel-> Remote MySQL®. Then run your application to connect.

Class.forName("com.mysql.jdbc.Driver");  
Connection con=DriverManager.getConnection(  
    "jdbc:mysql://www.xyz.com/abc","def","ghi");  
//here abc is database name, def is username and ghi        
Statement stmt=con.createStatement();  
ResultSet rs=stmt.executeQuery("select * from employee");  
    while(rs.next())  
        System.out.println(rs.getInt(1)+" "+rs.getString(2)+"  
                      "+rs.getString(3));  
con.close();    

Hope it will resolve your issue.

Thanks

1 Comment

Thanks, that helped me. I think adding % symbol as the host will allow all IPs.
2

Below queries, execution has fixed my problem:

sudo mysql

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

mysql> FLUSH PRIVILEGES;

Comments

1

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.

Comments

0

try this...

String password="Hsun123"; 

instead of

String password=""; 

Comments

0

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

Comments

0

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( 
            "jdbc:mysql://localhost:3306/Pravin","root","password"); 

No need to provide this password just insert ""

Complete Code is:

import java.sql.*;  
class TestDB{  
    public static void main(String args[]){  
    try{  
        Class.forName("com.mysql.jdbc.Driver");  
        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");  
        while(rs.next())  
            System.out.println(rs.getInt(1)+"  "+rs.getString(2)+" "+rs.getString(3));  
        con.close();  
    }catch(Exception e){
        System.out.println(e);  
    }  
}  

Comments

0

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
spring.jpa.show-sql= false
spring.jpa.hibernate.ddl-auto= update
#spring.jpa.hibernate.ddl-auto= create
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5Dialect
spring.main.banner-mode= off

1 Comment

This is exactly what I was looking for. Adding these connectors via Spring.* on my Spring project! Thank you for posting this!
0

Choose a password like password = 'Hellobds123@S'

Then run

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Hellobds123@S';

FLUSH PRIVILEGES;

Comments

-1

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.

Comments

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.