I have a database hosted at a remote location. I want to access that database. But I keep getting the error

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

This is the code that i have written

Connection con = null;
Class.forName("com.mysql.jdbc.Driver");
String username = "username";
String password = "password";
String server = "67.15.148.40"; //remote server address
String db = "data";
String url = "jdbc:mysql://"+server+"/"+db;
con = DriverManager.getConnection(url, username, password);

The username password I provide is correct as I can access the same databse using PHP and PHPmyAdmin.

Recommended Answers

All 6 Replies

Read the MySQL manual. The user administration portion. Pay close attention to the portion about Grants.

Read the MySQL manual. The user administration portion. Pay close attention to the portion about Grants.

I do have Grants to all the privileges and also grant to connect from any location. I am bale to connect using PHP, but not java, so I am sure have connecting privilege

I don't know too much about this PHP thing, but since PHP normally runs on a website, and that website can possibly be in the same place as the db, you have a different dynamic there. Now, all I can tell you is that this message is exactly what you get when the grants are not correct.

Then again, the message says database "db" and your code shows that the database should be "data". Recheck that as well (which would give the same error message since you will not have granted any access to the database "db").

I don't know too much about this PHP thing, but since PHP normally runs on a website, and that website can possibly be in the same place as the db, you have a different dynamic there. Now, all I can tell you is that this message is exactly what you get when the grants are not correct.

Then again, the message says database "db" and your code shows that the database should be "data". Recheck that as well (which would give the same error message since you will not have granted any access to the database "db").

Sorry The error message says could not connect to "data". And PHP webpage is on my local hard disk whereas database is on a database webserver. I have checked the grants on the database located at a webserver. It has remote access login privilege as well as the following privileges
Select
Insert
Update
Delete
Create
Drop
Referances
Index
Alter
Lock Table
Create Temp Table
Do I need to write http://<server ip> or only <serverip> in servername, as in this case server is at another location

No. That message says you are getting to the DB but that the DB is denying access.


Network problems would be something else, as would mistyped urls. You have either the wrong user, wrong pass, wrong db, or wrong grants.

commented: Thanks for current and fast help +2

No. That message says you are getting to the DB but that the DB is denying access.


Network problems would be something else, as would mistyped urls. You have either the wrong user, wrong pass, wrong db, or wrong grants.

Ok Now I think you are correct as If i enter wrong server address or wrong user or wrong password or wrong database then its result different error. This error indicates I have problem of grants. I will consult my database hosting vendor about this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.