MariaDB Connector/J Guide

Quickstart Guide for Connector/J

Quickstart Guide: MariaDB Connector/J

MariaDB Connector/J is the official Java Database Connectivity (JDBC) driver for connecting Java applications to MariaDB and MySQL databases. It allows Java programs to interact with databases using the standard JDBC API.

See About MariaDB Connector/J for full content.

1. Installation

You can include MariaDB Connector/J in your project using build tools like Maven or Gradle, or by manually adding the JAR file to your project's classpath.

a. Using Maven:

Add the following dependency to your pom.xml file:

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

b. Using Gradle:

Add the following dependency to your build.gradle file:

dependencies {
    implementation 'org.mariadb.jdbc:mariadb-java-client:3.3.3' // Use the latest stable version
}

c. Manual Installation:

  1. Download the latest stable .jar file from the MariaDB Downloads page.

  2. Add the downloaded .jar file to your project's classpath.

2. Basic Usage (Connecting to MariaDB)

Here's a simple Java example demonstrating how to establish a connection and execute a basic query using DriverManager.

Before Running:

  1. Replace your_database_name, your_username, your_password, and your_table_name with your actual database details.

  2. Ensure you have a MariaDB server running and a database/table set up.

3. Connection Strings

MariaDB Connector/J supports various connection string formats, including options for failover and load balancing. The basic format is:

jdbc:mariadb://<hostDescription>[,<hostDescription>...]/[database][?<key1>=<value1>[&<key2>=<value2>]]

For example:

  • jdbc:mariadb://localhost:3306/mydb

  • jdbc:mariadb://server1:3306,server2:3306/mydb?failover=true

4. Connection Pooling (for Production)

For production applications, it's highly recommended to use a connection pool to manage database connections efficiently. MariaDB Connector/J can be used with external connection pooling libraries like HikariCP or Apache Commons DBCP, or its own MariaDbPoolDataSource.

  • MariaDbDataSource: Creates a new connection each time.

  • MariaDbPoolDataSource: Maintains a pool of connections for reuse.

When using an external pool, configure it to use org.mariadb.jdbc.Driver as the JDBC driver class.

Last updated

Was this helpful?