1

I am trying to connect to MySQL from Java and I get the following error.

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:951) at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1717) at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1276) at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2395) at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2428) at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2213) at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:797) at com.mysql.jdbc.JDBC4Connection.(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:389) at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at com.javawebtutor.jsf.LoginBean.dbData(LoginBean.java:63) at com.javawebtutor.jsf.LoginBean.checkValidUser(LoginBean.java:80) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.el.parser.AstValue.invoke(AstValue.java:278) at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:273) at org.apache.jasper.el.JspMethodExpression.invoke(JspMethodExpression.java:70) at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) at javax.faces.component.UICommand.broadcast(UICommand.java:315) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2522) at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2511) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source) Exception Occured in the process :java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)"

Thank you for all help, I am very new to this. I am positive the password and username are correct so I am unsure where to go from here. Here is my code for connecting to the database:

package com.javawebtutor.jsf;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;


@ManagedBean(name="loginBean")
@SessionScoped
public class LoginBean {
private String userName;
private String password;
private String dbuserName;

private String dbpassword;
Connection connection;
Statement statement;
ResultSet resultSet;
String SQL;


public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getDbuserName() {
    return dbuserName;
}

public void setDbuserName(String dbuserName) {
    this.dbuserName = dbuserName;
}

public String getDbpassword() {
    return dbpassword;
}

public void setDbpassword(String dbpassword) {
    this.dbpassword = dbpassword;
}

public void dbData(String userName)
{
    Connection con = null;
    String conUrl = "jdbc:mysql://localhost:3306/test; user=root; password=pass;";
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        con = DriverManager.getConnection(conUrl);
        //connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test",user = "root","pass");
        statement = connection.createStatement();
        SQL = "Select * from USER_DETAIL where username like ('" + userName +"')";
        resultSet = statement.executeQuery(SQL);
        resultSet.next();
        dbuserName = resultSet.getString(1).toString();
        dbpassword = resultSet.getString(2).toString();
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
        System.out.println("Exception Occured in the process :" + ex);
    }
}

public String checkValidUser()
{
    dbData(userName);

    if(userName.equalsIgnoreCase(dbuserName))
    {

        if(password.equals(dbpassword))
            return "success";
        else
        {
            return "failure";
        }
    }
    else
    {
        return "failure";
    }
}
}
E-Riz
  • 31,431
  • 9
  • 97
  • 134
Octo
  • 53
  • 1
  • 7
  • Which DBMS are you using? You have mysql and sql server tagged. Your title says MySql and the question says sql server. They are NOT the same thing. – Sean Lange Nov 13 '15 at 14:30
  • I apologize, I am using MySQL Workbench and MySQL Server 5.7. – Octo Nov 13 '15 at 14:32
  • 3
    As a side note, you really should not be storing passwords in plain text. They should be salted and hashed. You also should parameterize your queries to avoid sql injection. – Sean Lange Nov 13 '15 at 14:33
  • Do you think this is the source of my problem? – Octo Nov 13 '15 at 14:41
  • 1
    NO, as I said that is a side note. It is something you need to fix but is not affecting your connection. You are getting an access denied. I am not a MySQL person (I work with sql server) but that sounds like the user does not have access to connect to the database. – Sean Lange Nov 13 '15 at 14:43
  • I see, my apologies once again. Thank you for your help. – Octo Nov 13 '15 at 14:48
  • This is a very typical MySQL error: you have to `GRANT` access to that \`user\`@\`localhost\`. This is done at the DBMS level. – Déjà vu Dec 17 '15 at 13:24

3 Answers3

1

In MySQL server administration, users are specified with both allowable hostnames and passwords. If the root user was not set up to include localhsot as a client, it will deny logins on the local machine. See this question and its accepted answer for some details.

Community
  • 1
  • 1
E-Riz
  • 31,431
  • 9
  • 97
  • 134
0

Can you try like this

con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","pass");

As getConnection function requires 3 arguments : url+dbname , username , password.

Vishnu
  • 479
  • 1
  • 3
  • 14
  • Still no luck unfortunately. – Octo Nov 13 '15 at 14:49
  • mysql -u root -p pass : can u give this command from terminal and tell if u r able to login to mysql ? – Vishnu Nov 13 '15 at 14:54
  • It prompts me to enter a password, I enter my password and then it says Error 1049 (42000): Unknown database 'password entered' 'password entered' being the password i entered, not what it actually says. – Octo Nov 13 '15 at 15:07
  • I switched to a different database and got in through the terminal, I switched which database in my code as well but still no luck with that. – Octo Nov 13 '15 at 16:05
0

Your password is wrong it seems.

You can try with empty password given below.

String conUrl = "jdbc:mysql://localhost:3306/test; user=root; password=;";
YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
Mukesh Kumar
  • 101
  • 3
  • 6