Adding MySQL to $PATH Environment Variable on OS X

Update, 09.10.2012: This just worked for me for adding Android’s adb tool to my environment path on OS X 10.6.8. The first time I tried I got an error because I wrote “export $PATH=” instead of “export PATH=”.

Earlier this year I took some Java courses. I learned a ton from what was on the syllabus. It was exciting to see stuff like Generics in the Collections framework in JDK 1.5 and compare it to how Actionscript didn’t have that option until very recently, when the Vector class was added to AS3 for Flash Player 10.

One thing that caught me by surprise was that all software installation instructions for class materials were for Windows only (may be I was naive to expect otherwise). The professor was good but he wasn’t a Mac guy and he was super busy at his day job. A few of us OS X guys had to do extra work on our own on top of all the regular class work, just to get Eclipse, Tomcat & MySQL working on our systems. As painful as it was to spend extra days on things like “Why do I have to spend days on learning UNIX when my Java homework is due tomorrow?!!!” OR “Why do I keep getting a 404 Error for Servlets but not for JSP?! !@##$#^$%^$%^!!”… in the end, I think we ended up as slightly better developers, with a little bit more experience, because we all had to teach ourselves a little Terminal and OS X UNIX commands on the spot.

At the very least, I can always say, “Hey, at least I taught myself how to add “mysql” to the $PATH Environment Variable on OS X:

  1. launch Terminal (assuming it’s a bash shell)
  2. write ‘echo $PATH’ to see what your path is now
  3. if ‘mysql’ isn’t in there, write in the path to where you installed it via this command:
    ‘export PATH=/Users/your_user_name/your_path_to_mysql/mysql/bin:$PATH >> ~/.bash_profile’
  4. that should add it to your bash_profile
  5. write ‘echo $PATH’ again, you should see ‘mysql’ in there

Note: this is for installing MySQL as a stand alone app for use with J2EE/Java projects. If you’re just trying to do some PHP on a development machine, it’s easier to use a wysiwyg LAMP package like MAMP or XAMPP.

More on the $PATH environment variable here.

The same technique can be used to add the open source Flex SDK compiler, MXMLC, to your $PATH environment variable on OS X.

Advertisements

How to set up Connector/J in Eclipse

Connector/J is a “Type IV JDBC driver for MySQL.”

Assumptions

You have downloaded & installed MySQL 5.1, Connector/J for 5.1 (the JDBC driver for MySQL) from mysql.com, Tomcat 5.5 or 6, Eclipse 3.4 for Java EE. I’m assuming this is a Dynamic Web Project in Eclipse with Tomcat 6 as the Target Runtime. I did this successfully on OS X 10.5 (Leopard) & on Windows XP.

The Steps:

  1. Select your project folder in Eclipse’s Project Explorer (just click on it so it’s highlighted).
  2. Open project properties. There’re several ways to do this. For example, you can go to Project > Properties in Eclipse’s top menu.
  3. Click on Java Build Path. Click “Add External Jars.” Navigate to where you unzipped Connector/J on your hard drive. Choose the .jar file (in my case mysql-connector-java-5.1.7-bin.jar) & click Open. It’ll now show up under JARs and class folders in the build path.
  4. Click on Java EE Module dependencies in the menu on the left.
  5. Check the checkbox next to the Connector/J path under JAR/Module. Hit Apply and OK.
    Run a skeleton test Servlet to see if it works:

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestJDBC extends HttpServlet {

    public void init(){
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            System.out.println("JDBC driver loaded");
        } catch (ClassNotFoundException e) {
            System.out.println(e.toString());
        } //Eclipse may auto-force some extra catch blocks here
     }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("Check the Console tab in" +
		" Eclipse... it should say \"JDBC driver loaded\"");
    }

}