Connect java to mysql using jdbc on osx

So, I added MySQL Connector / J 5.1.16 to my Build Path project. I use the standard OSX Java package and MAMP Pro 1.9.4 with MySQL 5.1.44 and Eclipse.

I created a simple java application 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 start, 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 without success. Any ideas how to fix this? The JDBC driver should be good, I tested it.

EDIT

I tried to run this through the console

 SnowCave:src stefanschipor$ java -cp $CLASSPATH test 

I get the same result as above

+6
source share
4 answers

Ok, this is just stupid. :)

You have opened MAMP, go to server> MySQL and uncheck the box "Allow only local access", which is set by default. This is strange, because what I am doing is local, but anyway ...

My program works, and the teams suggested by @JamesA also give the expected result!

Hurrah!

+13
source

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 connection 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 can also try a tool like DbVisualizer to check the URL of your connection.

+2
source

I also have this cryptic error in OSX and no solution was found on the first error. In my cases, this happens if the database structure was changed or the tables were dropped / created during development (MySQL as a data source for JBoss). In all cases, I can avoid the error if I disable MySQL after modifications and reboot it before starting JBoss.

0
source

you need to use Port localhost: 8889 or any other port that you find in MAMP> Settings> Ports> MySQL port. Then your connection will be successful!

0
source

Source: https://habr.com/ru/post/889773/


All Articles