Connect to MAMP MySql with Java JDBC

Probably the easiest way to develop PHP/MYSQL Applications on the Mac is to install MAMP. However, if you then want to add Java capabilities to this there is very little documentation that explains what you need to do. After much digging around its actually rather easy assuming you are using the JDBC connector – this is how.

Allow MYSQL Networking
JDBC connects to mysql via an IP Address rather than a socket so you need to allow for networking in the my.cnf file. If you are using MAMP PRO you can just boot MAMP Admin and go to File > Edit Template > my.cnf before finding the line that says skip_networking and just uncomment it. Normal MAMP requires a bit more work and this link should help. Restart your Apache/MySql services.

Set Up a Basic Java MySQL Connection
The following is an example java connection function:

Add the JDBC Library
If you right click on Libraries folder in the Project Window of Netbeans you should see an option to Add Library. Click this and scroll down until you find MySql JDBC. Select it and Bob is your uncle. Everything should run just Dandy now.

About CDB 360 Articles
Self-Employed Software Developer, Spark, Property Management, Hobby Forestry, Ex-Teacher, Engineering - Wood, Metal, Electrics & Computers. Outdoors - Walk, Cycle, Kitesurf,

3 Comments

  1. Hi Sunjay,

    I’m surprised you needed to uncheck ‘Allow local access only’ as this option limits you to localhost. Maybe you are not running as localhost on your machine but that would be odd as I thought it was default. Be aware this means your mysql server is open to attack from other machines on the network. Restricting access to local machine only is a standard way of securing mysql servers used in conjunction with web servers. Only websites hosted on the same machine can connect with the mysql server.

    Otherwise, glad you found the post helpful. Best Regards

  2. Thanks. Just a note: On my version on MAMP Pro the name of the variable in the my.cnf file was “MAMP_skip-networking_MAMP”

2 Trackbacks / Pingbacks

  1. Using Java with MySQL through MAMP | Paris Fox
  2. Using Java with MySQL through MAMP | JaScript

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.