Adding MySQL to JAVA on MAC
Written on 11-02-2009 – 17:58:24 | by Tom KeurHi,
Today I was trying to get MySQL working with JDBC on my Macbook pro.
After doing some research I found out that I needed to get “MySQL Connector/J” in the Java extension map.
Here are the steps to get MySQL working with JDBC.
- Download the MySQL Connect/J from the Mysql.com website.
- Unzip the package!
- Copy the “mysql-connector-java-5.1.7-bin.jar” file to your: /System/Library/Java/Extensions/ directory.
- You can now use MySQL with JDBC!
- You don’t need to add the jar file to your project in eclipse manually anymore!
Since I’m using Eclipse to develop with Java, it’s in my JRE System Library by default!

For testing if MySQl is working correctly I wrote a simple class.
SQL file:
CREATE TABLE IF NOT EXISTS `testtable` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `name` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL, `age` INT(11) NOT NULL, `city` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ; INSERT INTO `testtable` (`id`, `name`, `age`, `city`) VALUES (1, 'Mr. John Doe', 31, 'Groningen'), (2, 'Mrs. Jane Doe', 31, 'Groningen');
Java:
/** * @author tomkeur */ import java.sql.*; public class MySQLDatabase { public static void main(String[] args) { Connection conn = null; try { // SETTINGS: userName, passWord and dataBase. String userName = "root"; String passWord = ""; String dataBase = "test"; // Creating class. Class.forName("com.mysql.jdbc.Driver").newInstance(); // Let's create a connection. conn = DriverManager.getConnection("jdbc:mysql:///"+dataBase, userName, passWord); // Do we have a connection? if (!conn.isClosed()) { // We have a MySQL Connection. Statement s = conn.createStatement ( ); s.executeQuery ("SELECT * FROM testtable"); ResultSet rs = s.getResultSet ( ); while (rs.next()) // loop through rows of result set { int pkey = rs.getInt (1); String strName = rs.getString(2); int intAge = rs.getInt(3); String strCity = rs.getString(4); System.out.println ("uID: " + pkey + ", Name: " + strName + ", Age: " + intAge + ", City: " + strCity ); } rs.close(); // close result set s.close(); // close statement } } catch (Exception e) { System.err.println("Exception: " + e.getMessage()); } finally { // If we don't have a error, close the connection! try { if (conn != null) { conn.close(); } } catch (SQLException e) { } } } }
On Microsoft platsforms you can allso add the the jar file to:
C:\Program Files\Java\jreX.X.X_XX\lib\ext where the X’s are the version of your installed Java Runtime Environment (JRE).
2 Responses to “Adding MySQL to JAVA on MAC”
By Tubby on Feb 11, 2009 | Reply
Nice posting
The solution you suggesting is only one step less complex then the eclipse lib solution (you dont need to add it to the classpath of the project manually) _but_
- normally the choice whether you need MySQL or a other db is project specific
- deployment-wise you would like your 3rd party dependencies of your project gathered on a place so you can package then in a war(ideally) for deployment
- version-wise you can run into version conflicts for the same package faster if you add the to the System library (different projects requiring different versions of the same jar)
By Binyamin on May 24, 2009 | Reply
Thanks!
This post helped me very much, I didn’t manage to work with Connector/J for some time and now it works fine.
Thanks again,
Binyamin