com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorEx

2019-08-02 17:26发布

问题:

I want to connect to my database and create the tables using Java. Butm It throws below exception:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Access denied for user ''@'localhost' to database

回答1:

Check if user is allowed for remote connection, run below sql query on your database to allow your user to connect from everywhere.

Allow a user to connect from anywhere

`GRANT ALL ON *.* to <user-name>@'<ip>' IDENTIFIED BY '<password>'`

Sample for root user :-

GRANT ALL ON *.* to root@'%' IDENTIFIED BY 'root'; 
// grant root user to connect from everywhere(%) , which has root password.


回答2:

Have you add a new user in your database with all rights (write - read) ? Is the URL of your database correct ? For example like :

jdbc:mysql://localhost:8000


标签: java mysql jdbc