Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 354
GreenDude Avatar asked Jun 04 '11 16:06

GreenDude


People also ask

How do I download JDBC on Mac?

Downloading the JDBC Driver On your Mac client system, open a browser and log into the myVertica portal. Navigate to the Downloads page, scroll to the Client Software download section, and click the download link for the JDBC driver. Accept the license agreement and wait for the download to complete.

Does JDBC work with MySQL?

In Java, we can connect to our database(MySQL) with JDBC(Java Database Connectivity) through the Java code. JDBC is one of the standard APIs for database connectivity, using it we can easily run our query, statement, and also fetch data from the database.


3 Answers

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!

like image 129
GreenDude Avatar answered Sep 23 '22 01:09

GreenDude


Are you sure MySQL is running on port 3306?


If the mysql daemon is listening on port 3306 lsof -i :3306 should return:

COMMAND  PID        USER   FD   TYPE     DEVICE SIZE/OFF NODE NAME
mysqld  7616    username   10u  IPv6 0x1fbf6940      0t0  TCP *:mysql (LISTEN)

A direct connect to the port using nc localhost 3306 should return:

H
5.5.969]G.Mw4??9cfUY?k!^:D&mysql_native_password

where 5.5.969 is the mysql version number.


You could also try a tool like DbVisualizer to test your connection URL.

like image 44
James Allman Avatar answered Sep 21 '22 01:09

James Allman


you need to use the Port localhost:8889 or whatever port you find in MAMP > Settings > Ports > MySQL-Port. Then your Connection will succeed!

like image 29
heinrich.osudio Avatar answered Sep 19 '22 01:09

heinrich.osudio