鉴于此代码:
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
: ,但我仍然无法获得任何正确的输出。
知道有什么问题吗?
谢谢
备注: 为了清楚起见,我正在使用:
面向 Web 开发人员的 Eclipse Java EE IDE。
雄猫 7
Xampp