---
title: "How to Connect Java Applications to MariaDB Using JDBC"
publish_date: 2022-01-31
updated_date: 2022-02-01
author: "Alejandro Duarte"
channel:
  - name: "Developer"
    url: "/resources/blog/channel/developer.md"
---

# How to Connect Java Applications to MariaDB Using JDBC

If you are a developer who just discovered MariaDB, or want to get started with Java Database Connectivity (JDBC) and learn how to connect SQL databases with Java applications. Or perhaps you just need to grab the JDBC connection string (URL) to consume a MariaDB database or [SkySQL](https://staging-mdb.com/products/skysql) service. Worry not. I’ve got you covered!

Here’s a set of resources that I prepared for Java developers interested in getting started with MariaDB and JDBC. Everything you need: A [GitHub repository](https://github.com/mariadb-developers/java-quickstart), articles on DZone, Hacker Noon, and DEV; and [YouTube](https://www.youtube.com/mariadb) videos if you want to see the things in action!

### Just Show Me the Code!

Here’s a *TL;DR* section for you.

Add the JDBC Driver ([check](https://staging-mdb.com/docs/clients/mariadb-connectors/connector-j/#latest-software-releases) the latest version, and [check what’s new in version 3.0](https://staging-mdb.com/resources/blog/whats-new-in-mariadb-connector-j-3-0/)):

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

Open the connection (alternatively use a try-catch block to close the connection automatically):

```
Connection connection = DriverManager.getConnection(
    "jdbc:mariadb://localhost:3306/database_name",
    "user", "password"
);
```

If you are using SkySQL, use the following connection string:

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

Close the connection (if not using a try-catch block):

```
connection.close();
```

Create (`INSERT`), update (`UPDATE`), and delete (`DELETE`):

```
try (PreparedStatement statement = connection.prepareStatement("""
        INSERT INTO table_name(column1, column2)
        VALUES (?, ?)
      """)) {
    statement.setString(1, someString);
    statement.setInt(2, someInteger);
    int rowsInserted = statement.executeUpdate();
}
```

Read (`SELECT`):

```
try (PreparedStatement statement = connection.prepareStatement("""
            SELECT column1, column2
            FROM table_name
        """)) {
    ResultSet resultSet = statement.executeQuery();
    while (resultSet.next()) {
        String val1 = resultSet.getString(1); // by column index
        int val2 = resultSet.getInt("column2"); // by column name
        // ... use val1 and val2 ...
    }
}
```

If you want more detailed information, keep reading…

### Connecting to MariaDB from Java

JDBC is the foundational technology for connecting Java applications with SQL databases. Persistence frameworks such as [JPA](https://jakarta.ee/specifications/persistence/3.0)/[Hibernate](https://hibernate.org/), [MyBatis](https://mybatis.org/mybatis-3), and [jOOQ](https://www.jooq.org/) use JDBC under the hood, so understanding the key concepts in JDBC will put you in a good position to explore these persistence frameworks.

When you want to connect to a database from Java, you need a `Connection` object. I wrote a tutorial that explains how to get one of these objects through a JDBC driver (you’ll learn what a JDBC driver is as well):

- **JDBC Tutorial Part 1: Connecting to a Database** ([DZone](https://dzone.com/articles/jdbc-tutorial-part-1-connecting-to-a-database) | [Hacker Noon](https://hackernoon.com/jdbc-tutorial-part-1-how-to-connect-to-a-database) | [DEV](https://dev.to/alejandro_du/jdbc-tutorial-part-1-connecting-to-a-database-kfc))

Here’s the video version of the tutorial:

Source code: <https://github.com/mariadb-developers/java-quickstart/tree/main/jdbc/part1>

### Executing SQL Queries from Java

With a connection established, you can run SQL statements (queries or updates) using a `PreparedStatement` object. This tutorial explains how to do so in a safe way to avoid SQL injection attacks:

- **JDBC Tutorial Part 2: Running SQL Queries** ([DZone](https://dzone.com/articles/jdbc-tutorial-part-2-running-sql-queries) | [Hacker Noon](https://hackernoon.com/jdbc-tutorial-part-2-how-to-run-sql-queries) | [DEV](https://dev.to/alejandro_du/jdbc-tutorial-part-2-running-sql-queries-1lai))

Here’s the video version:

Source code: <https://github.com/mariadb-developers/java-quickstart/tree/main/jdbc/part2>

### Using Database Connection Pools

When you implement a web application, web service, or any other kind of application that allows multiple clients and connects to a database, you should use a *connection pool*. This tutorial explains what a connection pool is and how to use it:

- **JDBC Tutorial Part 3: Using database connection pools** ([DZone](https://dzone.com/articles/jdbc-tutorial-part-3-using-database-connection-poo) | [Hacker Noon](https://hackernoon.com/jdbc-tutorial-part-3-how-to-use-database-connection-pools) | [DEV](https://dev.to/alejandro_du/jdbc-tutorial-part-3-using-database-connection-pools-5cgg))

Here’s the video:

Source code: <https://github.com/mariadb-developers/java-quickstart/tree/main/jdbc/part3>

The [MariaDB connector](https://staging-mdb.com/docs/clients/mariadb-connectors/connector-j/) includes a connection pool implementation with great performance and features. [Check it out](https://staging-mdb.com/docs/clients/mariadb-connectors/connector-j/connection-pools/).

### GitHub Repository

All the code is available on a single [GitHub repository](https://github.com/mariadb-developers/java-quickstart). Look for the **jdbc** directory where you can find the code for each part of the tutorial as independent Maven projects. Refer to the instructions in each readme file for the steps to run the applications. I plan to add quickstart examples for JPA/Hibernate, MyBatis, and jOOQ in the future. Stay tuned!