Access denied for user 'root'@'localhost'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Norgy
    New Member
    • May 2013
    • 56

    Access denied for user 'root'@'localhost'

    Code:
    import java.sql.*;
    
    public class DBConnect {
        
        private Connection con;
        private Statement st;
        private ResultSet rs;
        
        public DBConnect(){
            try{
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/DataBaseName","user_name","password");
                st = con.createStatement();
                
            }catch(Exception ex){
                System.out.println("Error: "+ex);
            }
            
        }
        
        public void getData(){
            try{
                String query = "select * from  test";
                rs = st.executeQuery(query);
                System.out.println("Records from Database");
                while(rs.next()){
                    String name = rs.getString("name");
                    String age = rs.getString("age");
                    System.out.println("name: "+name+"  "+"age: "+age);
                }
            }catch(Exception ex){
                System.out.println(ex);
            }
        } 
        
    }
    i have watched a video demonstrating how to connect java netBeans to MySQL server
    this code is written in the video, but when i run it, it gives me the error:
    Error: java.sql.SQLExc eption: Access denied for user 'user_name'@'lo calhost' (using password: YES)
    java.lang.NullP ointerException
    what is the problem here ?
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    It means that you used the wrong user_name/password combination to attempt a connection to the database.

    Contact your DBA to determine the correct username and password.

    Comment

    • Norgy
      New Member
      • May 2013
      • 56

      #3
      but i log in to the server with the same user_name and password

      Comment

      • zmbd
        Recognized Expert Moderator Expert
        • Mar 2012
        • 5501

        #4
        You're trying to tell me that ","user_name"," password"); is your user name and password? I just figured that these were being used as place holders in the code instead of the real information (^_^)

        Doesn't matter, because the error is quite clear, the account name and password you have actually used in the code is not correct for the type of connection you are attempting establish, there is no point in arguing this fact.

        Contact your DBA to determine the correct username and password.

        Comment

        • zmbd
          Recognized Expert Moderator Expert
          • Mar 2012
          • 5501

          #5
          Sorry, something just occured to me, it's been a very long time since IT setup my test MYSQL database:

          Verify that your MYSQL has a passphrase go to the directory where you installed mysql, and in the bin directory try to bring up mysql prompt using:
          Code:
          ./mysql –u root mysql
          If the login goes through without any problem, it means no passphrase is set.
          You can use mysqadmin script to set/reset passphrase.
          Code:
          ./mysqladmin –u root passphrase "new passphrase"

          Comment

          • Norgy
            New Member
            • May 2013
            • 56

            #6
            no ^_^ i mean that i sign in to the server with the same user name and password written in the code (which are not written here, i wrote only alternative names for them)
            anyway, i made a new account with new password and edited the code with the new ones but nothing changed :(
            and sometimes it gives me this error
            Error: com.mysql.jdbc. exceptions.jdbc 4.Communication sException: Communications link failure

            The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. java.lang.NullP ointerException

            Comment

            • Norgy
              New Member
              • May 2013
              • 56

              #7
              sorry, i have just noticed your last comment
              actually, i don't understand what you mean by bringing up mysql prompt.

              Comment

              • iAirplane
                New Member
                • Feb 2015
                • 1

                #8
                Originally posted by Norgy
                sorry, i have just noticed your last comment
                actually, i don't understand what you mean by bringing up mysql prompt.
                I have the same problem at the moment, I'm sure my password and username is correct but I get the same error, while with a different database on the same server I can connect with the same user.

                Since this is a year old, have you solved the problem, and can you help me?

                Comment

                Working...