Documentation Home
MySQL Connector/J 8.0 Developer Guide
Related Documentation Download this Manual
PDF (US Ltr) - 458.0Kb
PDF (A4) - 460.3Kb
HTML Download (TGZ) - 110.5Kb
HTML Download (Zip) - 128.1Kb


MySQL Connector/J 8.0 Developer Guide  /  Connector/J Installation  /  Installing the Driver and Configuring the CLASSPATH

4.2 Installing the Driver and Configuring the CLASSPATH

Once you have extracted the distribution archive, you can install the driver by placing mysql-connector-java-version.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 the JVM.

To use the driver with the JDBC DriverManager, use com.mysql.cj.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 the user's .profile, .login or other login file, or 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.jar:$CLASSPATH

# C shell (csh, tcsh):
shell> setenv CLASSPATH /path/mysql-connector-java-ver.jar:$CLASSPATH

For Windows platforms, you set the environment variable through the System Control Panel.

To use the X DevAPI features in Connector/J, you also need the external library protobuf-java-2.6.0.jar, which you can download from any official Maven repository, for example https://repo1.maven.org/maven2/com/google/protobuf/protobuf-java/2.6.0/, and add it to the CLASSPATH.

If you prefer, you can use Maven dependencies manager to install and configure the Connector/J library in your project. Connector/J is published in The Maven Central Repository with "GroupId: mysql" and "ArtifactId: mysql-connector-java," and can be linked to your project by adding the following dependency in your pom.xml file:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>x.y.z</version>
</dependency>

Note that if you choose use Maven to manage your project dependencies, you do not need to explicitly refer to the library protobuf-java as it is resolved by dependency transitivity. However, if you do not want to use the X DevAPI features, you may also want to add a dependency exclusion to avoid linking the unneeded sub-library. For example:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>x.y.z</version>
    <exclusions>
        <exclusion>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
        </exclusion>
    </exclusions> 
</dependency>

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 8, Connection Pooling with Connector/J, Section 9.3, “Configuring Load Balancing with Connector/J”, and Section 9.5, “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.cj.jdbc package, if your J2EE application server supports or requires them. The javax.sql.XADataSource interface is implemented using the com.mysql.cj.jdbc.MysqlXADataSource class, which supports XA distributed transactions.

The various MysqlDataSource classes support the following parameters (through standard set mutators):

  • user

  • password

  • serverName (see the previous section about failover hosts)

  • databaseName

  • port


User Comments
Sign Up Login You must be logged in to post a comment.