Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
482 views
in Technique[技术] by (71.8m points)

connect java to mysql using jdbc on osx

So I added the MySQL Connector/J 5.1.16 to my project's Build Path. I'm using the default OSX Java package and MAMP Pro 1.9.4 with MySQL 5.1.44 and Eclipse.

I've set up a simple java app with the following function:

private static String dbUrl = "jdbc:mysql://127.0.0.1:3306/mpp";
private static String dbUsername = "root";
private static String dbPassword = "root";

private Statement statement = null;

private void dbConnect() {


try {
    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection(dbUrl, dbUsername, dbPassword);
    statement = connection.createStatement();

} catch(SQLException e) {
    System.err.print(e.getMessage() + " ARGH!");
} catch(Exception e) {
    System.err.print(e.getMessage() + " FUUUUUUUUUU!");
}
}

When I run it I get the following error:

Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server. ARGH!

I googled and searched for about an hour with no success. Any ideas on how to fix this? The JDBC driver should be fine, I kinda tested it.

EDIT

I tried running this through the console

SnowCave:src stefanschipor$ java -cp $CLASSPATH test

I get the same output as above

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

OK, this is just silly. :)

You have open up MAMP, go to Server > MySQL and uncheck "Allow local access only" which is checked by default. This is weird since what I'm doing is local, but anyways...

My program seems to work and the commands suggested by @JamesA also yield the expected output!

Huzzah!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...