DML with MariaDB Connector/R2DBC and Spring Data

Overview

Java developers can use MariaDB Connector/R2DBC to connect to MariaDB Enterprise using the Reactive Relational Database Connectivity (R2DBC) API. R2DBC operations are non-blocking, which makes it more scalable than Java's standard JDBC API. MariaDB Connector/R2DBC can be used with the very popular Spring Data framework, which can provide support for repositories, object mapping, and transaction management.

DML Operations

DML (Data Manipulation Language) refers to all SQL-data statements in the SQL standard (ISO/IEC 9075-2:2016), for example, DELETE, INSERT, REPLACE, SELECT, and UPDATE.

With Spring Data, DML operations can be performed by invoking the following methods:

Method

Description

DatabaseClient.execute(String sql)

Execute any DML statement.

DatabaseClient.select()

Execute a SELECT statement.

DatabaseClient.insert()

Execute a INSERT statement.

DatabaseClient.update()

Execute a UPDATE statement.

DatabaseClient.delete()

Execute a DELETE statement.

Code Example: INSERT, UPDATE, DELETE

INSERT, UPDATE, and DELETE are DML (Data Manipulation Language) operations that modify the data in a table.

The following example shows how to insert data into the example table created in Set up the Environment.

To update or delete data, replace the INSERT statement in the code example with an UPDATE, or DELETE statement:

//Module Imports
import org.mariadb.r2dbc.MariadbConnectionConfiguration;
import org.mariadb.r2dbc.MariadbConnectionFactory;
import org.springframework.data.r2dbc.core.DatabaseClient;

import reactor.test.StepVerifier;

// Main Application Class
public class App {

   // Connection Configuration
   private static MariadbConnectionConfiguration conf;
   private static MariadbConnectionFactory connFactory;

   private static DatabaseClient client;

   // Main Process
   public static void main(String[] args) {

      try {
         conf = MariadbConnectionConfiguration.builder()
               .host("192.0.2.1")
               .port(3306)
               .username("connr2dbc_test")
               .password("passwd")
               .database("test")
               .build();

         // Instantiate a Connection Factory
         connFactory = new MariadbConnectionFactory(conf);

         // Instantiate a Database Client
         client = DatabaseClient.create(connFactory);

         // Add First Contact
         client.insert()
            .into(Contact.class)
            .using(new Contact(1, "Walker", "Percy", "w.percy@example.com"))
            .then()
            .as(StepVerifier::create)
            .verifyComplete();

         // Add Second Contact
         client.insert()
            .into(Contact.class)
            .using(new Contact(2, "Flannery", "O'Connor", "f.oconnor@example.com"))
            .then()
            .as(StepVerifier::create)
            .verifyComplete();

         // Add Third Contact
         client.insert()
            .into(Contact.class)
            .using(new Contact(3, "Edgar", "Poe", "e.poe@example.com"))
            .then()
            .as(StepVerifier::create)
            .verifyComplete();

      } catch (IllegalArgumentException e) {
         e.printStackTrace();
      } catch (io.r2dbc.spi.R2dbcNonTransientResourceException e) {
         e.printStackTrace();
      }
   }
}
  • To update or delete data, use the update() or delete() methods, instead of the insert() method.

  • To execute a specific DML statement, use the execute() method.

Confirm the data was properly inserted by using MariaDB Client to execute a SELECT statement.

Example output:

SELECT * from test.contact;
+----+------------+-----------+------------------------+
| id | first_name | last_name | email                  |
+----+------------+-----------+------------------------+
|  1 | John       | Smith     | john.smith@example.com |
+----+------------+-----------+------------------------+

Code Example: SELECT

SELECT is a DML (Data Manipulation Language) operation that reads the data from a table.

The following example shows how to select data from the example table created in Setup for Examples:

//Module Imports
import org.mariadb.r2dbc.MariadbConnectionConfiguration;
import org.mariadb.r2dbc.MariadbConnectionFactory;
import org.springframework.data.r2dbc.core.DatabaseClient;
import reactor.test.StepVerifier;

// Main Application Class
public class App {

   // Connection Configuration
   private static MariadbConnectionConfiguration conf;
   private static MariadbConnectionFactory connFactory;

   private static DatabaseClient client;

   // Main Process
   public static void main(String[] args) {

      try {
         conf = MariadbConnectionConfiguration.builder()
               .host("192.0.2.1")
               .port(3306)
               .username("connr2dbc_test")
               .password("passwd")
               .database("test")
               .build();

         // Instantiate a Connection Factory
         connFactory = new MariadbConnectionFactory(conf);

         // Instantiate a Database Client
         client = DatabaseClient.create(connFactory);

         // Select all rows
         client.select()
            .from(Contact.class)
            .fetch().all()
            .doOnNext(it -> System.out.println(it))
            .as(StepVerifier::create)
            .expectNextCount(3)
            .verifyComplete();

         // Select the first row
         client.select()
            .from(Contact.class)
            .fetch().first()
            .doOnNext(it -> System.out.println(it))
            .as(StepVerifier::create)
            .expectNextCount(1)
            .verifyComplete();

         // Select all rows with explicit query
         client.execute("SELECT id, first_name,last_name,email FROM contact")
            .as(Contact.class)
            .filter(s -> s.fetchSize(25))
            .fetch().all()
            .doOnNext(it -> System.out.println(it))
            .as(StepVerifier::create)
            .expectNextCount(3)
            .verifyComplete();

         // Select single column
         client.select()
            .from(Contact.class)
            .map((row, rowMetadata) -> row.get("first_name", String.class))
            .all()
            .doOnNext(it -> System.out.println(it))
            .as(StepVerifier::create)
            .expectNextCount(3)
            .verifyComplete();

      } catch (IllegalArgumentException e) {
         e.printStackTrace();
      } catch (io.r2dbc.spi.R2dbcNonTransientResourceException e) {
         e.printStackTrace();
      }
   }
}

Example output:

//Output from first query
Contact [id=1, first_name=Walker, last_name=Percy, email=w.percy@example.com]
Contact [id=2, first_name=Flannery, last_name=O'Connor, email=f.oconnor@example.com]
Contact [id=3, first_name=Edgar, last_name=Poe, email=e.poe@example.com]

//Output from second query
Contact [id=1, first_name=Walker, last_name=Percy, email=w.percy@example.com]

//Output from third query
Contact [id=1, first_name=Walker, last_name=Percy, email=w.percy@example.com]
Contact [id=2, first_name=Flannery, last_name=O'Connor, email=f.oconnor@example.com]
Contact [id=3, first_name=Edgar, last_name=Poe, email=e.poe@example.com]

//Output from fourth query
Walker
Flannery
Edgar