Once you have extracted the distribution archive, you can
install the driver by placing
mysql-connector-java-[version]-bin.jar
in
your classpath, either by adding the full path to it to your
CLASSPATH
environment variable, or by
directly specifying it with the command line switch -cp when
starting your JVM.
If you are going to use the driver with the JDBC DriverManager,
you would use com.mysql.jdbc.Driver
as the
class that implements java.sql.Driver
.
You can set the CLASSPATH
environment
variable under UNIX, Linux or Mac OS X either locally for a user
within their .profile
,
.login
or other login file. You can also set
it globally by editing the global
/etc/profile
file.
For example, under a C shell (csh, tcsh) you would add the
Connector/J driver to your CLASSPATH
using
the following:
shell> setenv CLASSPATH /path/mysql-connector-java-[ver]-bin.jar:$CLASSPATH
Or with a Bourne-compatible shell (sh, ksh, bash):
shell> export set CLASSPATH=/path/mysql-connector-java-[ver]-bin.jar:$CLASSPATH
Within Windows 2000, Windows XP, Windows Server 2003 and Windows Vista, you must set the environment variable through the System Control Panel.
If you want to use MySQL Connector/J with an application server
such as GlassFish, Tomcat or JBoss, you will have to read your
vendor's documentation for more information on how to configure
third-party class libraries, as most application servers ignore
the CLASSPATH
environment variable. For
configuration examples for some J2EE application servers, see
Section 20.4.5.2, “Using Connector/J with J2EE and Other Java Frameworks”. However, the
authoritative source for JDBC connection pool configuration
information for your particular application server is the
documentation for that application server.
If you are developing servlets or JSPs, and your application server is J2EE-compliant, you can put the driver's .jar file in the WEB-INF/lib subdirectory of your webapp, as this is a standard location for third party class libraries in J2EE web applications.
You can also use the MysqlDataSource
or
MysqlConnectionPoolDataSource
classes in
the com.mysql.jdbc.jdbc2.optional
package, if
your J2EE application server supports or requires them. Starting
with Connector/J 5.0.0, the
javax.sql.XADataSource
interface is
implemented via the
com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
class, which supports XA distributed transactions when used in
combination with MySQL server version 5.0.
The various MysqlDataSource
classes
support the following parameters (through standard set
mutators):
user
password
serverName (see the previous section about fail-over hosts)
databaseName
port
User Comments
For JDK releases, I found a way to get it working by placing the .jar into the %insertPathToJava%/jre/lib/ext/. I tried this with 1.6.0u2 on XP and the program began compiling, without the class path workaround.
Copying the connector jar into your Java installation does work, but is not ideal because it will probably break if you install a java update (since that installs a whole new VM).
If you don't want to mess around with CLASSPATH, you could always include the code directly with your application; the 'unjar' Ant task is useful for this. Create a target which unjars library classes into your build tree before your jar/war target, and you'll be all done.
For Mac OS X, it seems that copying the MySQL driver .jar file into /Library/Java/Extensions is the only thing to do.
What finally worked for me, I was trying to get OpenOffice to connect to MySQL DB, was to place a symbolic link in the jre1.6.0_17/lib/ext/ directory.
On Linux, untar or unzip, mysql-connector-java-5.0.X any where on the system, I used /usr/java. I then created a symbolic link 'ln -s /usr/java/mysql-connector-java-5.0.8/mysql-connector-java-5.0.8-bin.jar /usr/java/latest/lib/ext/'. Once OpenOffice was pointed to the currect version of java, everything worked.
The trick for me was learning about the extensions directory. In this case /usr/java/jre1.6.0_17/lib/ext/.
For Mac OS X, I was getting 'Driver not suitable' exception. I've noticed the extracted jar file permissions were set to 655 and should be 755 which has fixed the problem for me.
Add your own comment.