我一直在尝试连接到我的 MySQL 数据库,但它不会连接到它。运行我的应用程序时,我使用 mysql-java-connector.jar 运行它,但它仍然无法工作。
这是我正在使用的连接方法:
public static void createConnection() {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();//opens class
String IP="localhost:3306";//connection ip
String DB="SECRET";//database name
String User="SECRET";//username
String Pass="SECRET"; //password
con = DriverManager.getConnection("jdbc:mysql://"+IP +"/"+DB, User, Pass); //creates connection
stmt = con.createStatement();
} catch (Exception e) {//catches if connection failed
System.out.println("Connection to SQL database failed!");//tells you it failed @ the run.bat
e.printStackTrace();
}
}
变量:
public static Connection con = null;
public static Statement stmt;
public static boolean connectionMade;
这是我尝试连接到我的数据库时遇到的错误:
java.sql.SQLException: Access denied for user 'daniel_voteuser'@'localhost' (using password: YES)
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)Connection to SQL database failed!
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 com.rs.database.mysql.impl.Highscores.createConnection(Highscores.java:34)
at com.rs.Launcher.main(Launcher.java:113)`
谁能帮我解决这个错误?几天来我一直试图解决这个问题,但我还没有成功:(