在osx上使用jdbc将java连接到mysql [英] connect java to mysql using jdbc on osx

查看:145
本文介绍了在osx上使用jdbc将java连接到mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我将MySQL Connector / J 5.1.16添加到我的项目的Build Path中。我正在使用默认的OSX Java包和MAMP Pro 1.9.4与MySQL 5.1.44和Eclipse。

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.

我已经设置了一个简单的Java应用程序,其功能如下:

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!

我用Google搜索并搜索了大约一个小时但没有成功。有想法该怎么解决这个吗? JDBC驱动程序应该没问题,我对它进行了测试。

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.

编辑

我尝试通过控制台运行它

I tried running this through the console

SnowCave:src stefanschipor$ java -cp $CLASSPATH test

我得到与上面相同的输出

I get the same output as above

推荐答案

好的,这真是太傻了。 :)

OK, this is just silly. :)

您已打开MAMP,转到服务器> MySQL并取消选中仅允许本地访问,默认情况下已选中。这很奇怪,因为我正在做的是本地的,但无论如何...

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...

我的程序似乎工作,@JamesA建议的命令也产生预期的输出!

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

Huzzah!

这篇关于在osx上使用jdbc将java连接到mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆