I'm trying to create a background service so i create a normal java project. Here is the code i try to connect to database:
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/test_db", "root", "root");
Statement stmt = con.createStatement();
But everytime i run this code, it failed:
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:127)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:862)
at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:444)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
The problem is that using the same code on another maven project of mine, it work. So there should be no problem with the url, username, password nor user permission ( There already other project connect to same db, using same user but hibernate instead ). I think there might be some library missing so i copy all jar file exported from maven project and add into this java project but it still fail to connect to database. I also try to change my mysql-connector to newest version, the same still happened.
So can anyone tell me any possible reason for this access denied? Thank you.
================================================================ Update log error when change to connector version 5.1.8
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3536)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3468)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:917)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3974)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1282)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2142)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:773)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
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:406)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:352)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
================================================================================ Update select from mysql.user table
+---------+-----------+
| user | host |
+---------+-----------+
| newuser | localhost |
| root | localhost |
+---------+-----------+
2 rows in set (0.00 sec)
========================================================================== Answer: Ok this problem was resolved. Because all of those people come and mark this as duplicated, it prevent me from answer my own question. So if someone ever got this stupid mistake like me, i will put the answer here:
My project encoding was set to UTF-16 by default. I forgot to change this to UTF-8 like normally
When i store my password somewhere in my project ( java file, properties file ), this password also in UTF-16, so its actual different from what i saw on my tool.
When the application read the password and send it to mysql, authen fail because the password is incorrect.
=> Update the project and file, set encoding to UTF-8 resolve my problem.