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.

17
  • 1
    did you try and search? Commented May 29, 2018 at 2: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. Commented May 29, 2018 at 6: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. Commented May 29, 2018 at 6: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 Commented May 29, 2018 at 6: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 Commented May 29, 2018 at 6:56

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.