Table of Contents
MySQL Connector/J is distributed as a .zip
or
.tar.gz
archive, available for download from
the Connector/J
Download page. The archive contains the sources and the
JAR archive named
mysql-connector-java-
.
version
-bin.jar
You can install the Connector/J package using either the binary or
source distribution. The binary distribution provides the easiest
method for installation; the source distribution lets you
customize your installation further. With either solution, you
manually add the Connector/J location to your Java
CLASSPATH
.
If you are upgrading from a previous version, read the upgrade information in Section 3.3, “Upgrading from an Older Version” before continuing.
Connector/J is also available as part of the Maven project. For more information and to download the Connector/J JAR files, see the Maven repository.
For the easiest method of installation, use the binary
distribution of the Connector/J package. Extract the JAR archive
from the tar/gzip or zip archive to a suitable location, then
optionally make the information about the JAR archive available
by changing your CLASSPATH
(see
Section 3.2, “Installing the Driver and Configuring the CLASSPATH
”).
Use the appropriate graphical or command-line utility to extract the distribution (for example, WinZip for the .zip archive, and tar for the .tar.gz archive). Because there are potentially long file names in the distribution, we use the GNU tar archive format. Use GNU tar (or an application that understands the GNU tar archive format) to unpack the .tar.gz variant of the distribution.
Once you have extracted the distribution archive, you can
install the driver by placing
mysql-connector-java-
in your classpath, either by adding the full path to
it to your version
-bin.jar
CLASSPATH
environment variable, or
by directly specifying it with the command line switch
-cp
when starting the JVM.
To use the driver with the JDBC
DriverManager
, use
com.mysql.jdbc.Driver
as the class that
implements java.sql.Driver
.
You can set the CLASSPATH
environment
variable under Unix, Linux, or 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, add the Connector/J driver to your
CLASSPATH
using one of the following forms,
depending on your command shell:
# Bourne-compatible shell (sh, ksh, bash, zsh): shell> export CLASSPATH=/path/mysql-connector-java-ver
-bin.jar:$CLASSPATH # C shell (csh, tcsh): shell> setenv CLASSPATH /path/mysql-connector-java-ver
-bin.jar:$CLASSPATH
For Windows platforms, you set the environment variable through the System Control Panel.
To use MySQL Connector/J with an application server such as
GlassFish, Tomcat, or JBoss, 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
Chapter 7, Connection Pooling with Connector/J,
Section 8.2, “Configuring Load Balancing with Connector/J”,
and
Section 8.4, “Advanced Load-balancing and Failover Configuration”.
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 using the
com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
class, which supports XA distributed transactions when used in
combination with MySQL server version 5.0 and later.
The various MysqlDataSource
classes
support the following parameters (through standard set
mutators):
user
password
serverName
(see the previous section
about failover hosts)
databaseName
port
This section has information for users who are upgrading from one version of Connector/J to another, or to a new version of the MySQL server that supports a more recent level of JDBC. A newer version of Connector/J might include changes to support new features, improve existing functionality, or comply with new standards.
In Connector/J 5.0.x and earlier, the alias for a table in
a SELECT
statement is
returned when accessing the result set metadata using
ResultSetMetaData.getColumnName()
.
This behavior however is not JDBC compliant, and in
Connector/J 5.1, this behavior has been changed so that
the original table name, rather than the alias, is
returned.
The JDBC-compliant behavior is designed to let API users
reconstruct the DML statement based on the metadata within
ResultSet
and
ResultSetMetaData
.
You can get the alias for a column in a result set by
calling
ResultSetMetaData.getColumnLabel()
.
To use the old noncompliant behavior with
ResultSetMetaData.getColumnName()
,
use the useOldAliasMetadataBehavior
option and set the value to true
.
In Connector/J 5.0.x, the default value of
useOldAliasMetadataBehavior
was
true
, but in Connector/J 5.1 this was
changed to a default value of false
.
Using the UTF-8 Character Encoding -
Prior to MySQL server version 4.1, the UTF-8 character
encoding was not supported by the server, however the JDBC
driver could use it, allowing storage of multiple
character sets in latin1
tables on the
server.
Starting with MySQL-4.1, this functionality is deprecated. If you have applications that rely on this functionality, and can not upgrade them to use the official Unicode character support in MySQL server version 4.1 or newer, add the following property to your connection URL:
useOldUTF8Behavior=true
Server-side Prepared Statements - Connector/J 3.1 will automatically detect and use server-side prepared statements when they are available (MySQL server version 4.1.0 and newer). If your application encounters issues with server-side prepared statements, you can revert to the older client-side emulated prepared statement code that is still presently used for MySQL servers older than 4.1.0 with the following connection property:
useServerPrepStmts=false
Connector/J 3.1 is designed to be backward-compatible with
Connector/J 3.0 as much as possible. Major changes are
isolated to new functionality exposed in MySQL-4.1 and newer,
which includes Unicode character sets, server-side prepared
statements, SQLState
codes returned in
error messages by the server and various performance
enhancements that can be enabled or disabled using
configuration properties.
Unicode Character Sets:
See the next section, as well as
Character Set Support, for information on this MySQL
feature. If you have something misconfigured, it will
usually show up as an error with a message similar to
Illegal mix of collations
.
Server-side Prepared Statements: Connector/J 3.1 will automatically detect and use server-side prepared statements when they are available (MySQL server version 4.1.0 and newer).
Starting with version 3.1.7, the driver scans SQL you are
preparing using all variants of
Connection.prepareStatement()
to
determine if it is a supported type of statement to
prepare on the server side, and if it is not supported by
the server, it instead prepares it as a client-side
emulated prepared statement. You can disable this feature
by passing
emulateUnsupportedPstmts=false
in your
JDBC URL.
If your application encounters issues with server-side
prepared statements, you can revert to the older
client-side emulated prepared statement code that is still
presently used for MySQL servers older than 4.1.0 with the
connection property
useServerPrepStmts=false
.
Datetimes with all-zero
components (0000-00-00 ...
): These
values cannot be represented reliably in Java. Connector/J
3.0.x always converted them to NULL
when being read from a ResultSet.
Connector/J 3.1 throws an exception by default when these
values are encountered, as this is the most correct
behavior according to the JDBC and SQL standards. This
behavior can be modified using the
zeroDateTimeBehavior
configuration
property. The permissible values are:
exception
(the default), which
throws an SQLException with an SQLState of
S1009
.
convertToNull
, which returns
NULL
instead of the date.
round
, which rounds the date to the
nearest closest value which is
0001-01-01
.
Starting with Connector/J 3.1.7,
ResultSet.getString()
can be decoupled
from this behavior using
noDatetimeStringSync=true
(the default
value is false
) so that you can
retrieve the unaltered all-zero value as a String. Note
that this also precludes using any time zone conversions,
therefore the driver will not allow you to enable
noDatetimeStringSync
and
useTimezone
at the same time.
New SQLState Codes:
Connector/J 3.1 uses SQL:1999 SQLState codes returned by
the MySQL server (if supported), which are different from
the legacy X/Open state codes that Connector/J 3.0 uses.
If connected to a MySQL server older than MySQL-4.1.0 (the
oldest version to return SQLStates as part of the error
code), the driver will use a built-in mapping. You can
revert to the old mapping by using the configuration
property useSqlStateCodes=false
.
ResultSet.getString()
:
Calling ResultSet.getString()
on a
BLOB
column will now return
the address of the byte[]
array that
represents it, instead of a String
representation of the BLOB
.
BLOB
values have no
character set, so they cannot be converted to
java.lang.String
s without data loss or
corruption.
To store strings in MySQL with LOB behavior, use one of
the TEXT
types, which the
driver will treat as a java.sql.Clob
.
Debug builds: Starting
with Connector/J 3.1.8 a debug build of the driver in a
file named
mysql-connector-java-
is shipped alongside the normal binary jar file that is
named
version
-bin-g.jarmysql-connector-java-
.
version
-bin.jar
Starting with Connector/J 3.1.9, we do not ship the
.class
files unbundled, they are only
available in the JAR archives that ship with the driver.
Do not use the debug build of the driver unless instructed
to do so when reporting a problem or bug, as it is not
designed to be run in production environments, and will
have adverse performance impact when used. The debug
binary also depends on the Aspect/J runtime library, which
is located in the
src/lib/aspectjrt.jar
file that comes
with the Connector/J distribution.
To just get MySQL Connector/J up and running on your system, install Connector/J using a standard binary release distribution. Instructions in this section is only for users who, for various reasons, want to compile Connector/J from source.
The requirements and steps for installing from source Connector/J 5.1.37 or later, 5.1.34 to 5.1.36, and 5.1.33 or earlier are different; check the section below that is relevant for the version you want.
Installing Connector/J 5.1.37 or later from source. To install MySQL Connector/J from its source tree on GitHub, you need to have the following software on your system:
A Git client, to check out the sources from our GitHub repository (available from http://git-scm.com/downloads).
Apache Ant version 1.8.2 or newer (available from http://ant.apache.org/).
JDK 1.8.x AND JDK 1.5.x.
JRE 1.6.x (optional)
JUnit libraries (available from https://github.com/junit-team/junit/wiki/Download-and-Install).
The required .jar
files from the
Hibernate ORM 4.1 or 4.2 Final release bundle, which is
available at
http://sourceforge.net/projects/hibernate/files/hibernate4/.
To check out and compile MySQL Connector/J, follow these steps:
Check out the code from the source code repository for MySQL Connector/J located on GitHub at https://github.com/mysql/mysql-connector-j; for the latest release of the Connector/J 5.1 series, use the following command:
shell> git clone https://github.com/mysql/mysql-connector-j.git
To check out a release other than the latest one, use the
--branch
option to specify the revision tag
for it:
shell> git clone --branch 5.1 https://github.com/mysql/mysql-connector-j.git
Under the current directory, the commands create a
mysql-connector-j
subdirectory , which
contains the code you want.
Make sure that you have both JDK 1.8.x AND JDK 1.5.x installed. You need both JDKs because besides supporting JDBC from 4.0 to 4.2, Connector/J 5.1 also supports JDBC 3.0, which is an older version and requires the older JDK 1.5.x.
Consider also having JRE 1.6.x installed. This is optional:
if JRE 1.6.x is not available or not supplied to Ant with
the property com.mysql.jdbc.java6.rtjar
,
the Java 8 bootstrap classes will be used. A warning will be
returned, saying that the bootstrap class path was not set
with the option to compile sources written for Java 6.
Place the required junit.jar
file in a
separate directory—for example,
/home/username/ant-extralibs
.
In the same directory for extra libraries described in the
last step, create a directory named
hibernate4
, and put under it all the
.jar
files you can find under the
/lib/required/
folder in the Hibernate
ORM 4 Final release bundle.
Change your current working directory to the
mysql-connector-j
directory created in
step 1 above.
In the directory, create a file named
build.properties
to indicate to Ant the
locations of the root directories for your JDK 1.8.x and JDK
1.5.x installations, the location of the
rt.jar
of your JRE 1.6.x (optional),
and the location of the extra libraries. The file should
contain the following property settings, with the
“path_to_*
” parts
replaced by the appropriate filepaths:
com.mysql.jdbc.jdk8=path_to_jdk_1.8
com.mysql.jdbc.jdk5=path_to_jdk_1.5
com.mysql.jdbc.java6.rtjar=path_to_rt.jar_under_jre_1.6
/rt.jar com.mysql.jdbc.extra.libs=path_to_folder_for_extra_libraries
Alternatively, you can set the values of those properties
through the Ant -D
options.
Issue the following command to compile the driver and create
a .jar
file for Connector/J:
shell> ant dist
This creates a build
directory in the
current directory, where all the build output goes. A
directory is created under the build
directory, whose name includes the version number of the
release you are building. That directory contains the
sources, the compiled .class
files, and
a .jar
file for deployment. For more
information and other possible targets, including those that
create a fully packaged distribution, issue the following
command:
shell> ant -projecthelp
Install the newly created .jar
file for
the JDBC driver as you would install a binary
.jar
file you download from MySQL by
following the instructions given in
Section 3.2, “Installing the Driver and Configuring the CLASSPATH
”.
Note that a package containing both the binary and source code for Connector/J 5.1 can also be downloaded from the Connector/J Download page.
Installing Connector/J 5.1.34 to 5.1.36 from source. To install MySQL Connector/J 5.1.34 to 5.1.36 from the Connector/J source tree on GitHub, make sure that you have the following software on your system:
A Git client, to check out the sources from our GitHub repository (available from http://git-scm.com/downloads).
Apache Ant version 1.8.2 or newer (available from http://ant.apache.org/).
JDK 1.6.x AND JDK 1.5.x.
JUnit libraries (available from https://github.com/junit-team/junit/wiki/Download-and-Install).
The required .jar
files from the
Hibernate ORM 4.1 or 4.2 Final release bundle, which is
available at
http://sourceforge.net/projects/hibernate/files/hibernate4/.
To check out and compile MySQL Connector/J, follow these steps:
Check out the code from the source code repository for MySQL
Connector/J located on GitHub at
https://github.com/mysql/mysql-connector-j,
using the --branch
option to specify the
revision tag for release 5.1.xx
:
shell> git clone --branch 5.1.xx
https://github.com/mysql/mysql-connector-j.git
Under the current directory, the commands create a
mysql-connector-j
subdirectory , which
contains the code you want.
Make sure that you have both JDK 1.6.x AND JDK 1.5.x installed. You need both JDKs because Connector/J 5.1 supports both JDBC 3.0 (which has existed prior to JDK 1.6.x) and JDBC 4.0.
Place the required junit.jar
file in a
separate directory—for example,
/home/username/ant-extralibs
.
In the same directory for extra libraries described in the
last step, create a directory named
hibernate4
, and put under it all the
.jar
files you can find under the
/lib/required/
folder in the Hibernate
ORM 4 Final release bundle.
Change your current working directory to the
mysql-connector-j
directory created in
step 1 above.
In the directory, create a file named
build.properties
to indicate to Ant the
locations of the root directories for your JDK 1.5.x and JDK
1.6.x installations, as well as the location of the extra
libraries. The file should contain the following property
settings, with the
“path_to_*
” parts
replaced by the appropriate filepaths:
com.mysql.jdbc.jdk5=path_to_jdk_1.5
com.mysql.jdbc.jdk6=path_to_jdk_1.6
com.mysql.jdbc.extra.libs=path_to_folder_for_extra_libraries
Alternatively, you can set the values of those properties
through the Ant -D
options.
Issue the following command to compile the driver and create
a .jar
file for Connector/J:
shell> ant dist
This creates a build
directory in the
current directory, where all the build output goes. A
directory is created under the build
directory, whose name includes the version number of the
release you are building. That directory contains the
sources, the compiled .class
files, and
a .jar
file for deployment. For more
information and other possible targets, including those that
create a fully packaged distribution, issue the following
command:
shell> ant -projecthelp
Install the newly created .jar
file for
the JDBC driver as you would install a binary
.jar
file you download from MySQL by
following the instructions given in
Section 3.2, “Installing the Driver and Configuring the CLASSPATH
”.
Installing Connector/J 5.1.33 or earlier from the source tree. To install MySQL Connector/J 5.1.33 or earlier from the Connector/J source tree on GitHub, make sure that you have the following software on your system:
A Git client, to check out the source code from our GitHub repository (available from http://git-scm.com/downloads).
Apache Ant version 1.7 or newer (available from http://ant.apache.org/).
JDK 1.6.x AND JDK 1.5.x. Refer to Section 2.2, “Java Versions Supported” for the version of Java you need to build or run any Connector/J release.
The Ant Contrib (version 1.03b is available from http://sourceforge.net/projects/ant-contrib/files/ant-contrib/1.0b3/) and JUnit (available from https://github.com/junit-team/junit/wiki/Download-and-Install) libraries.
The required .jar
files from the
Hibernate ORM 4.1 or 4.2 Final release bundle, which is
available at
http://sourceforge.net/projects/hibernate/files/hibernate4/.
To check out and compile a specific branch of MySQL Connector/J, follow these steps:
Check out the code from the source code repository for MySQL
Connector/J located on GitHub at
https://github.com/mysql/mysql-connector-j,
using the --branch
option to specify the
revision tag for release 5.1.xx
:
shell> git clone --branch 5.1.xx
https://github.com/mysql/mysql-connector-j.git
Under the current directory, the commands create a
mysql-connector-j
subdirectory , which
contains the code you want.
To build Connector/J 5.1, make sure that you have both JDK
1.6.x AND JDK 1.5.x installed. You need
both JDKs because Connector/J 5.1 supports both JDBC 3.0
(which has existed prior to JDK 1.6.x) and JDBC 4.0. Set
your JAVA_HOME
environment variable to
the path to the JDK 1.5.x installation.
Place the required ant-contrib.jar
file
(in exactly that name, without the version number in it;
rename the jar
file if needed) and
junit.jar
file in a separate
directory—for example,
/home/username/ant-extralibs
.
In the same directory for extra libraries described in the
last step, create a directory named
hibernate4
, and put under it all the
.jar
files you can find under the
/lib/required/
folder in the Hibernate
ORM 4 Final release bundle.
Change your current working directory to the
mysql-connector-j
directory created in
step 1 above.
In the directory, create a file named
build.properties
to indicate to Ant the
locations of the Javac and rt.jar
of
your JDK 1.6.x, as well as the location of the extra
libraries. The file should contain the following property
settings, with the
“path_to_*
” parts
replaced by the appropriate filepaths:
com.mysql.jdbc.java6.javac=path_to_javac_1.6
/javac com.mysql.jdbc.java6.rtjar=path_to_rt.jar_under_jdk_1.6
/rt.jar com.mysql.jdbc.extra.libs=path_to_folder_for_extra_libraries
Alternatively, you can set the values of those properties
through the Ant -D
options.
Issue the following command to compile the driver and create
a .jar
file for Connector/J:
shell> ant dist
This creates a build
directory in the
current directory, where all the build output goes. A
directory is created under the build
directory, whose name includes the version number of the
release you are building. That directory contains the
sources, the compiled .class
files, and
a .jar
file for deployment. For more
information and other possible targets, including those that
create a fully packaged distribution, issue the following
command:
shell> ant -projecthelp
Install the newly created .jar
file for
the JDBC driver as you would install a binary
.jar
file you download from MySQL by
following the instructions given in
Section 3.2, “Installing the Driver and Configuring the CLASSPATH
”.
The Connector/J source code repository or packages that are shipped with source code include an extensive test suite, containing test cases that can be executed independently. The test cases are divided into the following categories:
Functional or unit tests: Classes
from the package testsuite.simple
.
Include test code for the main features of the
Connector/J.
Performance tests: Classes from the
package testsuite.perf
. Include
test code to make measurements for the performance of
Connector/J.
Fabric tests: Classes from the
package testsuite.fabric
. Includes
the code to test Fabric-specific features. These tests
require the setting of some special properties that are
not documented here. Consult the code or the
Fabric-related targets in the bundled Ant build file,
build.xml
.
Regression tests: Classes from the
package testsuite.regression
.
Includes code for testing bug and regression fixes.
The bundled Ant build file contains targets like
test
and test-multijvm
,
which can facilitate the process of running the Connector/J
tests; see the target descriptions in the build file for
details. Besides the requirements for building Connector/J from
the source code described in
Section 3.4, “Installing from Source”, a number of the
tests also require the File System Service Provider 1.2 for the
Java Naming and Directory Interface (JNDI), available at
http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-java-plat-419418.html)—place
the jar files downloaded from there into the
lib
directory or in the directory pointed
to by the property com.mysql.jdbc.extra.libs
.
To run the test using Ant, in addition to the properties
required for Section 3.4, “Installing from Source”,
you must set the following properties in the
build.properties
file or through the Ant
-D
options:
com.mysql.jdbc.testsuite.url
: it
specifies the JDBC URL for connection to a MySQL test
server; see
Section 5.1, “Driver/Datasource Class Names, URL Syntax and Configuration Properties
for Connector/J”.
com.mysql.jdbc.testsuite.jvm
: the JVM
to be used for the tests. If the property is set, the
specified JVM will be used for all test cases except if it
points to a Java 5 directory, in which case any test cases
for JDBC 4.0 and later are run with the JVM supplied with
the property com.mysql.jdbc.jdk8
(for
5.1.36 and earlier, supplied with the property
com.mysql.jdbc.jdk6
). If the property
is not set, the JVM supplied with
com.mysql.jdbc.jdk5
will be used to run
test cases for JDBC 3.0 and the one supplied with
com.mysql.jdbc.jdk8
(for 5.1.36 and
earlier, supplied with the property
com.mysql.jdbc.jdk6
) will be used to
run test cases for JDBC 4.0 and later.
After setting these parameters, run the tests with Ant in the following ways:
Building the test
target with
ant test
runs all test cases by default
on a single server instance. If you want to run a
particular test case, put the test's fully qualified class
names in the test
variable; for
example:
shell > ant -Dtest=testsuite.simple.StringUtilsTest test
You can also run individual tests in a test case by
specifying the names of the corresponding methods in the
methods
variable, separating multiple
methods by commas; for example:
shell > ant -Dtest=testsuite.simple.StringUtilsTest -Dmethods=testIndexOfIgnoreCase,testGetBytes test
Building the test-multijvm
target with
ant test-multijvm
runs all the test
cases
using multiple JVMs of different versions on multiple
server instances. For example, if you want to run the
tests using a Java 7 and a Java 8 JVM on three server
instances with different configurations, you will need to
use the following properties:
com.mysql.jdbc.testsuite.jvm.1=path_to_Java_7
com.mysql.jdbc.testsuite.jvm.2=path_to_Java_8
com.mysql.jdbc.testsuite.url.1=URL_to_1st_server
com.mysql.jdbc.testsuite.url.2=URL_to_2nd_server
com.mysql.jdbc.testsuite.url.3=URL_to_3rd_server
Unlike the target test
, the target
test-multijvm
only recognizes the
properties
com.mysql.jdbc.testsuite.jvm.
and
N
com.mysql.jdbc.testsuite.url.
,
where N
N
is a numeric suffice; the
same properties without the suffices are ignored by
test-multijvm
. As with the target
test
, if any of the
com.mysql.jdbc.testsuite.jvm.
settings points to Java 5, then Ant relies on the property
N
com.mysql.jdbc.jdk8
to run the tests
specific to JDBC 4.0 and later.
You can choose to run individual test cases or specific
tests by using the test
or
methods
property, as explained in the
last bullet for the target test
. Each
test is run once per possible combination of JVMs and
server instances (that is, 6 times in total for in this
example).
When a test for a certain JVM-server combination has
failed, test-multijvm
does not throw an
error, but moves on to the next combination, until all
tests for all combinations are finished.
While the test results are partially reported by the console, complete reports in HTML and XML formats are provided:
For results of test
: view the HTML
report by opening
build/junit/unitregress/report/index.html
.
XML version of the reports are located in the folder
build/junit/unitregress
.
For results of test-multijvm
: view the
HTML report for each JVM-server combination by opening
build/junit/MySQL
.
XML version of the reports are located in the folder
N
.server_version
/operating_system_version
/jvm-version
/unitregress/report/index.htmlbuild/junit/MySQL
.
N
.server_version
/operating_system_version
/jvm-version
/unitregress