Connecting to MariaDB databases using Java

You have two options to connect to MariaDB from Java:

  • Java Database Connectivity API (JDBC): An API that allows Java applications use SQL databases. JDBC is the foundation technology used by persistence frameworks such as JPA, Hibernate, Spring Data, MyBatis, and jOOQ.

  • Reactive Relational Database Connectivity (R2DBC): A specification for reactive, non-blocking database drivers.

Using JDBC

Add the JDBC Driver (check the latest version):

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

Connect to the database and run a query as follows:

try (Connection connection = DriverManager.getConnection(
        "jdbc:mariadb://localhost:3306/database_name",
        "user", "password")
) {
    Statement statement = conn.createStatement();
    ResultSet rs = statement.executeQuery("SELECT name, email FROM contact");
    while (rs.next()) {
        System.out.print(rs.getInt("name") + " " + rs.getInt("email"));
    }
} catch (SQLException e) {
    e.printStackTrace();
}

If you are connecting to a SkySQL instance use the following connection string:

jdbc:mariadb://example.skysql.net:5001/database_name?useSsl=true&serverSslCert=/path/to/skysql_chain.pem

Check the documentation and the Developer Code Central on GitHub for more information and examples on how to connect to MariaDB databases using Java persistence frameworks.

Using R2DBC

Add the R2DBC Driver (check the latest version):

<dependency>
    <groupId>org.mariadb</groupId>
    <artifactId>r2dbc-mariadb</artifactId>
    <version>1.0.3</version>
</dependency>

Add the Reactor dependency:

<dependency>
    <groupId>io.projectreactor</groupId>
    <artifactId>reactor-core</artifactId>
    <version>3.4.15</version>
</dependency>

Alternatively, you can use plain Reactive Streams, RxJava, or other reactive libraries or frameworks.

Connect to the database and run a query as follows:

ConnectionFactory connectionFactory = ConnectionFactories.get(
    ConnectionFactoryOptions.builder()
        .option(ConnectionFactoryOptions.DRIVER, "mariadb")
        .option(ConnectionFactoryOptions.HOST, "localhost")
        .option(ConnectionFactoryOptions.PORT, 3306)
        .option(ConnectionFactoryOptions.DATABASE, "database_name")
        .option(ConnectionFactoryOptions.USER, "user")
        .option(ConnectionFactoryOptions.PASSWORD, "password")
        .build()
);

Mono.from(connectionFactory.create())
        .flatMapMany(connection -> connection
                .createStatement("SELECT name, email FROM contact")
                .execute())
        .flatMap(result -> result
                .map((row, rowMetadata) -> "" + row.get("name") + row.get("email")))
        .doOnNext(System.out::println)
        .doOnError(Throwable::printStackTrace)
        .subscribe();

Thread.currentThread().join(1000); // wait for output

If you are connecting to a SkySQL instance use the following:

r2dbc:mariadb://example.skysql.net:5001/database_name?sslMode=VERIFY_CA&&serverSslCert=/path/to/skysql_chain.pem

Check the documentation and the Developer Code Central on GitHub for more information and examples on how to connect to MariaDB databases using Java persistence frameworks.

Need more help?

If you have questions, comments, or are looking for more information on MariaDB, please check out:

You can also reach out to us via: