gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 23:45:20 25 4
gpt4 key购买 nike

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

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)"

谢谢大家的帮助,我对此很陌生。我肯定密码和用户名是正确的,所以我不确定从这里去哪里。这是我连接到数据库的代码:

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

最佳答案

在 MySQL 服务器管理中,用户是 specified with both allowable hostnames and passwords .如果 root 用户未设置为包括 localhsot 作为客户端,它将拒绝在本地计算机上登录。参见 this question and its accepted answer了解一些细节。

关于java - 为什么从 Java 连接到 MySQL 数据库会被拒绝访问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33695000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com