我遇到连接MySQL的问题,我在谷歌和本网站上寻找过,并且有解决方案可以解决这个问题,但它们在我的代码中不起作用。 这就是代码:
public static String host = SQL.getInstance().getConfig().getString("Config.SQLhost");
public static String port = SQL.getInstance().getConfig().getString("Config.SQLport");
public static String database = SQL.getInstance().getConfig().getString("Config.SQLdatabase");
public static String username = SQL.getInstance().getConfig().getString("Config.SQLusername");
public static String password = SQL.getInstance().getConfig().getString("Config.SQLpassword");
public static Connection con;
public static void connect(){
if(!isConnected()){
try {
con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
System.out.println("[SQL] Connected with MySQL.");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static boolean isConnected(){
return (con == null ? false : true);
}
这是错误报告:
java.sql.SQLException: Access denied for user 'localhost'@'localhost' (using password: YES)
[21:04:26警告]:at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:959) [21:04:26警告]:在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3870) [21:04:26警告]:在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3806) [21:04:26警告]:在com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:871) [21:04:26警告]:在com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1686) [21:04:26警告]:在com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1207) [21:04:26警告]:在com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2254) [21:04:26警告]:在com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2285) [21:04:26警告]:在com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2084) [21:04:26警告]:在com.mysql.jdbc.ConnectionImpl。(ConnectionImpl.java:795) [21:04:26警告]:在com.mysql.jdbc.JDBC4Connection。(JDBC4Connection.java:44) [21:04:26警告]:at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) [21:04:26警告]:at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) [21:04:26警告]:at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) [21:04:26警告]:在java.lang.reflect.Constructor.newInstance(未知来源) [21:04:26警告]:在com.mysql.jdbc.Util.handleNewInstance(Util.java:404) [21:04:26警告]:在com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400) [21:04:26警告]:在com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327) [21:04:26警告]:在java.sql.DriverManager.getConnection(未知来源) [21:04:26警告]:在java.sql.DriverManager.getConnection(未知来源)
我希望你能帮助我。问候,Curbe。
答案 0 :(得分:0)
根据堆栈跟踪,您尝试使用'localhost'@'localhost'登录。我想这应该是:
localhost@localhost
此外,您应该将变量声明为私有,如:
private static String username = SQL.getInstance().getConfig().getString("Config.SQLusername");
答案 1 :(得分:0)
请查看下面的更新代码。
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MySqlConnection {
// Replace credentials with correct values
private static String host = "localhost";
private static String port = "3306";
private static String database = "test";
private static String username = "root";
private static String password = "root";
private static Connection con;
public static void main(String[] args) throws ClassNotFoundException{
if(!isConnected()){
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password);
System.out.println("[SQL] Connected with MySQL.");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static boolean isConnected(){
return (con == null ? false : true);
}
}
不要忘记将mysql-connector-java-5.0.8-bin.jar添加到您的项目中。还应声明局部变量private
。