0

i ran into a weird problem when connecting to mysql using java.

i'm running xampp with tomcat

i tried connecting to mysql from java. on my machine it works fine, but on a friends machine i get an error: access denied for user 'tomcat'@'localhost'

thing is - i managed to connect with the exact same info using php.

the java code for the connection is:

    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "mta_db";
    String driver = "com.mysql.jdbc.Driver";
    String userName = "tomcat";
    String password = "tomcat";
    try {
        Class.forName(driver).newInstance();
        _conn = DriverManager.getConnection(url + dbName, userName, password);
    } catch (Exception e) {
        e.printStackTrace();
    }

port is fine

any idea what could be the cause?

2
  • 1
    What does SHOW GRANTS say in mysql? Commented Oct 21, 2011 at 14:56
  • premissions are fine. i can connect through php with no problem. SHOW GRANTS returns 2 rows with GRANT ALL PRIVILEGES ON . TO 'root'@'localhost' WITH GRANT OPTION, GRANT PROXY ON "@" TO 'root'@'localhost' WITH GRANT OPTION. i tried user root also Commented Oct 21, 2011 at 15:01

1 Answer 1

1

The error is probably on the MySQL side, that is, the tomcat user doesnt have privileges to connect from non-localhost.

For simplicities sake you can first create an account that is able to connect from anywhere, via the MySQL GRANT statement like so:

GRANT ALL ON mta_db.* to tomcat@'%' identified by 'yourpassword';

In this case the % is a wildcard for any host.

If you know the actual IPs of your connecting clients you can later crack it down, to be more security conscious.

Sign up to request clarification or add additional context in comments.

1 Comment

does not work. it connects fine from PHP btw, and i checked the privileges

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.