鉴于此代码:
package db;
import java.sql.*;
public class Main
{
public static void main (String[] args) throws ClassNotFoundException, SQLException
{
Class.forName("com.mysql.jdbc.Driver");
String string = "jdbc:mysql://localhost:3306/testdb";
Connection con = DriverManager.getConnection(string , "root" , "root");
PreparedStatement statement = con.prepareStatement("select * from name");
ResultSet result = statement.executeQuery();
while (result.next())
{
System.out.println(result.getString(1) + " " + result.getString(2));
}
}
}
我正在尝试连接mysql服务器,但我明白了:
Exception in thread "main" java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4006)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:919)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1694)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1244)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2397)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2430)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2215)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:813)
at com.mysql.jdbc.JDBC4Connection.<init>(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:399)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:334)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at db.Main.main(Main.java:14)
行14
就是:Connection con = DriverManager.getConnection(string , "root" , "root");
我确信当我安装MySql
时,我输入了密码root
。只想确认一下 ,
我在这里输入:,
并将密码更改为root
,如下所示:,但我仍然无法获得任何正确的输出。
知道它有什么问题吗?
谢谢
REMARK: 为了清楚起见,我正在使用:
面向Web开发人员的Eclipse Java EE IDE。
Tomcat 7
Xampp
答案 0 :(得分:2)
线程“main”java.sql.SQLException中的异常:拒绝访问 用户'root'@'localhost'(使用密码:YES)
当用户无权访问localhost时,会出现此错误。
您需要从localhost授予对root用户的访问权限。
正如你已经补充的那样。我怀疑问题在于password='NO'
与mySQL
设置的行。
答案 1 :(得分:0)
使用您的用户名和密码执行以下命令:
grant usage on *`.`* to `devuser@localhost` identified by '`devpassword`';
grant all privileges on `dev.` to `devuser@localhost`;
答案 2 :(得分:-1)
你的问题与phpMyAdmin.its只是一个接口无关。
除此之外,我认为你应该用password =“NO”删除行