0

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

enter image description here

+---------+-----------+
| 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.

mameo
  • 639
  • 8
  • 25
  • 1
    did you try and search? – Scary Wombat May 29 '18 at 02:31
  • @ScaryWombat: As i mention in my post, if i put the same code running in other project (maven), it work. Only normal java not working. It has nothing to do with permission. I already grant permission for this database user months ago when i work in another project and that project still working fine atm. Please read the post before marking this as duplicate. – mameo May 29 '18 at 06:22
  • @karmakaze not sure if your command is correct but it fail as "ERROR 1049 (42000): Unknown database '3306'". I change the port number and database name position and it work, login success, run a select query and it return result for me. – mameo May 29 '18 at 06:25
  • I suggest that you go back to the `com.mysql.jdbc.Driver` i.e. some variation on `mysql-connector-java-5.1.8-bin.jar` – Scary Wombat May 29 '18 at 06:40
  • @ScaryWombat: I already test with 6.0.5 and 8.0.11. Change to 5.1.8 and nothing change. Still working on my other maven project and fail on my plain java project – mameo May 29 '18 at 06:56
  • Based upon `com.mysql.cj.jdbc` I was wondering whether you are using the correct jar – Scary Wombat May 29 '18 at 06:57
  • @ScaryWombat if you really think there a problem with the lib then i update the error log when change to version 5.1.8. As you can see the error come from com.mysql.jdbc but still the same – mameo May 29 '18 at 07:15
  • for completeness, please provide the output of `select Host, User from mysql.user;` – Scary Wombat May 29 '18 at 07:26
  • @ScaryWombat add image run from workbench and result from command line. Just a note that the url string got change between both 127.0.0.1 and localhost already but both fail – mameo May 29 '18 at 07:43
  • are you sure your password is correct? – Scary Wombat May 29 '18 at 07:52
  • @ScaryWombat and i said using the same block of code, putting into another project of mine and it work. other project using different schema also running at the moment so there no way the password is incorrect. even put all of these value into properties file and read from there but nothign change – mameo May 29 '18 at 07:57
  • well let us know when you figure what is wrong. – Scary Wombat May 29 '18 at 08:06
  • @ScaryWombat Finally found the problem. My project configuration encoding was set to UTF-16 by default. So it must have read the password wrongly. Change default encoding back to UTF-8 resolved my problem. Really a stupid mistake of mine. – mameo May 29 '18 at 09:28
  • Happy for you. Pretty random – Scary Wombat May 29 '18 at 23:51
  • @mameo You're totally right, the lowercase `-p` is to prompt for password. The correct flag should either be uppercase `-P 3306` or spelled out with double-dash `--port 3306`. Updating my original comment. – karmakaze Jun 05 '18 at 05:24
  • Try entering `mysql -h 127.0.0.1 --port 3306 -D test_db -u root -p` on the command-line then enter the password root when prompted. If that fails, it has nothing to do with the code. – karmakaze Jun 05 '18 at 05:26
  • @karmakaze thank you but the problem is my project encoding, changing it to UTF-8 fixed it. – mameo Jun 05 '18 at 06:09

0 Answers0