Installing Java Support for MySQL
To be able to access a MySQL database from a Java application, you will need two things (aside from MySQL itself):
Installing Java itself is beyond the scope of this book but fortunately not a daunting task for the average user. Most developers will be satisfied with using the Java 2 Platform, Standard Edition (J2SE), which can be downloaded from http://java.sun.com. Mac OS X users benefit from Apple's tight integration of Java into the operating system (Mac OS X version 10.4 comes with version 1.4.2 of J2SE), whereas Windows has intermittently supported the technology over its evolution (you may have a version of the JDK already installed).
In this section, I will demonstrate how to make the MySQL Connector/J driver usable with your already-installed Java. As the most common source of difficulty involves your Java CLASSPATH (see the sidebar), these instructions will attempt to squash those problems in advance.
Simply put, the CLASSPATH is a list of directories where Java will search when compiling or running applications. If, like the scripts in this chapter, your Java application references an external class, then Java has to be able to find that class. So it'll look within the CLASSPATH directories for it. If Java finds the referenced class in one of those directories, great. If it doesn't, that's a problem, and a common one at that.
When using a third-party class like MySQL Connector/J, you have two options: install it within the CLASSPATH or modify the CLASSPATH to include the location of MySQL Connector/J. The first method is the most foolproof, which is what I describe in the accompanying instructions. As for the second method, this is something you can do:
For the current session only
When you compile a Java application
When you run a Java application
How you adjust the CLASSPATH depends upon which of these cases you prefer and the operating system being used. If you'd rather add MySQL Connector/J to your CLASSPATH (instead of the easy installation method I describe), search the Web for instructions particular to your operating system.
To install the MySQL Connector/J:
Make sure you've installed the JDK. This is available from Sun, although it may already be installed on your operating system. You can test if Java is installed, as well as see the version installed, by running java -version
) at the command line.
Figure 9.1. Confirm that Java is installed by having it report on its currently installed version.
Find Java's lib/ext directory. This is the most important step but, unfortunately, one for which I cannot offer universally specific instructions. It depends upon your operating system, your version of the JDK, and how it was installed. What you'll ultimately need to find is a folder called ext, which itself is located within a folder called lib, which is located within the Java installation folder. Use your operating system's find or search feature to hunt for it. On my Windows XP installation, this was C:\Sun\AppServer\jdk\jre\lib\ext. I also had a C:\Program Files\Java\jre1.5.0_06\lib\ext directory, so I placed a copy of MySQL Connector/J there as well. On my Mac OS X installation, this was /System/Library/Frameworks/JavaVM.framework/Versions/1.4.2/Home/lib/ext. On Unix and Linux, this may be /usr/lib/jvm/<version>/lib/ext.
Download the current version of the MySQL Connector/J from www.mysql.com
. On MySQL's downloads page, you'll find links to this product under the Drivers and Connectors
heading. You'll want to download the latest Generally Available (GA) release, marked as recommended
. At the time of this writing, that was version 3.1, with version 5 in development.
Expand the downloaded file. The file is available in either a .tar.gz or .zip format. Once you've expanded the downloaded file, you will have a folder containing many things, including the actual driver, which is a .jar file called something like mysql-connector-java-X.X.X-bin.jar.
Move the driver to its new location. Use Windows Explorer, the Finder, or whatever, to move the driver file (from Step 4) to the directory identified in Step 2.
For a deployed Web application, place the JDBC driver (the .jar file) in the WEB-INF/lib directory.
Be careful with all of your commands, class names, and filenames, as Java is case-sensitive.