Gre*_*ude 9 java mysql mamp jdbc
所以我将MySQL Connector/J 5.1.16添加到我的项目的Build Path中.我正在使用默认的OSX Java包和MAMP Pro 1.9.4与MySQL 5.1.44和Eclipse.
我已经使用以下函数设置了一个简单的Java应用程序:
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!");
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时,我收到以下错误:
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!
Run Code Online (Sandbox Code Playgroud)
我用Google搜索并搜索了大约一个小时但没有成功.有想法该怎么解决这个吗?JDBC驱动程序应该没问题,我对它进行了测试.
编辑
我尝试通过控制台运行它
SnowCave:src stefanschipor$ java -cp $CLASSPATH test
Run Code Online (Sandbox Code Playgroud)
我得到与上面相同的输出
Gre*_*ude 15
好的,这真是太傻了.:)
您已打开MAMP,转到Server> MySQL并取消选中"仅允许本地访问",默认情况下已选中.这很奇怪,因为我正在做的是本地的,但无论如何......
我的程序似乎工作,@JamesA建议的命令也产生预期的输出!
好哇!