我正在尝试连接到我的服务器的 MySQL,但我一直遇到同样的问题并且找不到类似的问题。我的代码片段如下:
public class MySqlDatabaseHandler {
Connection con = null;
Statement st = null;
ResultSet rs = null;
public MySqlDatabaseHandler(String url, String user, String password){
/*String url = "jdbc:mysql://192.168.1.80:3306/testdb";
String user = "root";
String password = "rootpass";*/
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(url, user, password);
st = con.createStatement();
}
catch (Exception e){
e.printStackTrace();
}
}
}
我收到的结果:
java.sql.SQLException: Access denied for user 'root'@'192.168.1.3' (using password: NO)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1074)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4120)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:4052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:925)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1704)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1250)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2465)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:822)
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:404)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at handleServer.includes.classes.Server.MySqlDatabaseHandler.<init>(MySqlDatabaseHandler.java:24)
从表面上看,我相信由于“(使用密码:否)”,密码永远不会被发送到服务器。
我什至尝试将 url 更改为“jdbc:mysql://192.168.1.80:3306/testdb?user=root&password=rootpass”并发现自己的结果相同。
MySql 允许远程连接到测试客户端(192.168.1.3),并且已经通过 telnet 进行了测试,但是在 JDBC 中遇到了这个问题。有谁看到我做错了什么?