我正在尝试使用JDBC和MySQL驱动程序连接到数据库 Struts2使用Eclipse IDE,但由于访问被拒绝,我收到错误,任何人都可以帮助我吗?提前致谢
pom.xml是
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>testdatabase</groupId>
<artifactId>testdb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.24</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.36</version>
</dependency>
</dependencies>
</project>
动作类是
public String execute()
{
Connection con=null;
String URL = "jdbc:mysql://localhost/hadoopinfo";
String ret = ERROR;
try
{
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded!");
}
catch(Exception e)
{
e.printStackTrace();
}
try {
con = DriverManager.getConnection(URL, "root", "root123");
System.out.println("Connected to DataBase");
}
catch(Exception e)
{
System.out.println("seems problem with either URL or
User and Password ");
e.printStackTrace();
}
错误是
Driver loaded!
seems problem with either URL or User and Password
java.sql.SQLException: Access denied for user 'root'@'localhost' (using
password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3847)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:871)
at`com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication
(MysqlIO.java:`1665)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1207)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2249)
at
`com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2280)`
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2079)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:794)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
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:400)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:399)
at
com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:325)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
答案 0 :(得分:0)
假设您可以通过命令提示符(在应用程序之外)进行连接
在sql编辑器中运行此查询,并检查结果
SELECT * FROM mysql.user
查看您是否有作为主持人localhost
和用户root
,
如果不是:创建一个
如果是:您的密码错误。
您使用%
作为主机连接到Mysql。更改主机localhost
注意:要重置root密码,请点击此链接https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html