MariaDB Connector/J

Overview

Java developers can connect to MariaDB SkySQL from their application using MariaDB Connector/J, which implements the JDBC API.

MariaDB Connector/J can be used with:

  • MariaDB Platform for Transactions

  • MariaDB Platform for Analytics

  • MariaDB Platform for Smart Transactions (HTAP)

  • MariaDB Platform for Distributed SQL

Connector

MariaDB Connector/J

Supported Versions

2.7
1.8

Programming Language

Java

Programming Language Version

Java 11, Java 8 (Connector/J 2.7)
Java 7 (Connector/J 1.8)

API

JDBC 4.2 (Connector/J 2.7)
JDBC 4.1 (Connector/J 1.8)

Supports TLS

Yes

Supports Connection Pools

Yes

License

GNU Lesser General Public License v2.1

MariaDB Connector/J installation and connection instructions follow. Additional information on MariaDB Connector/J can be found in the Connector/J reference.

Installing MariaDB Connector/J

MariaDB Connector/J is typically installed either by using Maven or by manually downloading the JAR file.

Installing MariaDB Connector/J via Maven

To use Maven, install MariaDB Connector/J by adding the following dependency to your pom.xml file:

<dependency>
   <groupId>org.mariadb.jdbc</groupId>
   <artifactId>mariadb-java-client</artifactId>
   <version>2.7.2</version>
</dependency>

Depending on what features you plan to use, you may need to add some additional dependencies to pom.xml.

To use connection pools with commons-dbcp2 from Apache Commons, add the following dependency to your pom.xml file as well:

<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-dbcp2</artifactId>
   <version>2.8.0</version>
</dependency>

Installing MariaDB Connector/J via JAR

To download the JAR file manually:

  1. Go to the MariaDB Connectors download page

  2. Within the "Product" drop-down, choose the "Java 8 connector" or "Java 7 connector".

  3. Click the "Download" button to download the JAR file.

  4. When the JAR file finishes downloading, place it into the relevant directory on your system.

  5. Confirm that the JAR file is in your Java CLASSPATH.

Connecting with MariaDB Connector/J

  1. Determine the connection information for your MariaDB SkySQL database service:

    What

    Where to find it

    connProperties
    user

    Default username in the Service Credentials view, or the username you created

    connProperties
    password

    Default password in the Service Credentials view, the password you set on the default user, or the password for the user you created

    connProperties
    serverSslCert

    Path to the skysql_chain.pem file containing the "Certificate Authority Chain" (Download)

    connProperties
    useSsl

    Set to the string "true" to enable TLS, which is required for SkySQL.

    getConnection URL
    host component

    Fully Qualified Domain Name in the Service Details view

    getConnection URL
    port component

    Read-Write Port or Read-Only Port in the Service Details view

    • Connection parameters can be set either in the Properties object or in the connection URL.

  2. In your Java code, connect to the database service by calling the DriverManager.getConnection(String url, Properties info) method. Connection parameters can be set either in the Properties object or in the connection URL.

    import java.sql.*;
    import java.util.Properties;
    
    public class MariaDB_Connection_Test {
    
       public static void main(String[] args) {
    
          Properties connProperties = new Properties();
    
          connProperties.setProperty("user", "db_user");
          connProperties.setProperty("password", "db_password");
          connProperties.setProperty("useSsl", "true");
          connProperties.setProperty("serverSslCert", "/path/to/skysql_chain.pem");
    
          try (Connection conn = DriverManager.getConnection(
                "jdbc:mariadb://example.skysql.net:5001/",
                connProperties)) {
    
             // Use the connection
             // ...
          } catch (Exception e) {
    
             e.printStackTrace();
          }
       }
    }
    
  3. Make sure that your Java CLASSPATH includes the path to your new Java code as well as the path to MariaDB Connector/J.

    For example, you can set the Java CLASSPATH on Linux by executing the following:

    $ export CLASSPATH='/path/to/application/:/path/to/mariadb-java-client-2.7.2.jar'
    
  4. Compile your Java code:

    $ javac MariaDB_Connection_Test.java
    
  5. Execute your Java class:

    $ java MariaDB_Connection_Test