为什么从Java连接到MySQL数据库会被拒绝访问?

时间:2015-11-13 14:29:16

标签: java mysql mysql-connector

我正在尝试从Java连接到MySQL,我收到以下错误。

  

java.sql.SQLException:拒绝用户&root;' @' localhost' (使用密码:是)       在com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)       在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4096)       在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4028)       在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:951)       在com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1717)       在com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1276)       在com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2395)       在com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2428)       在com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2213)       在com.mysql.jdbc.ConnectionImpl。(ConnectionImpl.java:797)       在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)       在com.mysql.jdbc.Util.handleNewInstance(Util.java:411)       在com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:389)       在com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:305)       at java.sql.DriverManager.getConnection(Unknown Source)       at java.sql.DriverManager.getConnection(Unknown Source)       在com.javawebtutor.jsf.LoginBean.dbData(LoginBean.java:63)       在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)       在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)       在javax.faces.component.UICommand.broadcast(UICommand.java:315)       在javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)       在javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)       at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)       在com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)       在com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)       在javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)       在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)       在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)       在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)       在org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423)       在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079)       at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:625)       在org.apache.tomcat.util.net.AprEndpoint $ SocketProcessor.doRun(AprEndpoint.java:2522)       在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)       在java.lang.Thread.run(未知来源)   异常在进程中发生:java.sql.SQLException:拒绝用户&root;' @' localhost' (使用密码:是)"

感谢您的帮助,我对此非常陌生。我很肯定密码和用户名都是正确的,所以我不确定从哪里开始。这是我连接数据库的代码:

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";
    }
}
}

3 个答案:

答案 0 :(得分:1)

在MySQL服务器管理中,用户为specified with both allowable hostnames and passwords。如果root用户未设置为包含localhsot作为客户端,则会拒绝本地计算机上的登录。有关详细信息,请参阅this question and its accepted answer

答案 1 :(得分:0)

你能尝试这样吗

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

由于getConnection函数需要3个参数:url + dbname,username,password。

答案 2 :(得分:0)

您的密码似乎有误。

您可以尝试下面给出的空密码。

String conUrl = "jdbc:mysql://localhost:3306/test; user=root; password=;";