Question: I'm banging my head hard.
This simple line of code:

```
DriverManager.getConnection("jdbc:mysql://machine1:3306/information_schema", "user1", "password");

```

Running on machine1 results in an error:

```
1045 Access denied for user 'user1'@'localhost' (using password: YES)

```

However there's no problem running on `machine2` or any other machine for that matter.

`user1` actually has two different passwords, one is `password` if the connection is made to `machine1` (which is the machine name), one is `password1` if the connection is made to `localhost`.

That sounds crazy but our IT department insists that it is for security purposes. So if you can ignore the madness of that, the problem is obvious: I asked JDBC to connect to `machine1` with username `user1` and password `password`, which is correct, but it somehow tries to connect to `localhost` and hence the error.

I suspect that on `machine1`, there's some sort of configuration that delegates all internal connection from `machine1` to `localhost`. That's why the error only occurs when I run that command from `machine1`, and never occurs if I run it from elsewhere.

Modify the code to connect to `localhost` will work but the framework does not allow me to do that.

Any idea?

Answer: 
I hate this when I bang my head against the wall for 5 hours and the moment after I post the question on SO I find the answer.

The problem is that in /etc/host, `machine1` points to the same address as localhost (127.0.0.1). Modify the file to have `machine1` point to the real IP address (e.g. `10.240.3.167`) will solve the problem.
Are you running this code on machine1? In that case the error is correct because localhost is itself. Have you tried connecting via the MySQL command line from the same machine running your code? I suspect the credentials aren't set up in MySQL to allow connections from localhost. 

I just re-read. You need to use the localhost password when connecting from machine1. MySQL uses @localhost as where the connection is coming from. What value jdbc used for dns is not relevant.