Java Connector Using Maven

MariaDB Connector/J is used to connect applications developed in Java to MariaDB and MySQL databases using the standard JDBC API.


  • A MariaDB / MySQL server running on localhost using the default port 3306
  • Java version >= 8
  • Maven

Create Maven Project

Create a simple Java project with Maven:

mvn archetype:generate -DartifactId=my-app 
-DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false

Replace "" and "my-app" with appropriate values

The new project will be created in the folder "my-app". This folder contains the file pom.xml that permits configuring Maven.

Get MariaDB Java Driver

Declares driver in pom.xml (and setting java minimal version to 1.8) :

pom.file will then be :

<project xmlns="" xmlns:xsi=""






Standard JDBC methods DriverManager.getConnection(String url, String user, String password) are used to connect to the database.

Basic url string is standardized for the MariaDB driver : jdbc:(mysql|mariadb):[replication:|failover:|sequential:|aurora:]//<hostDescription>[,<hostDescription>...]/[database][?<key1>=<value1>[&<key2>=<value2>]]

The MariaDB driver is registered automatically for a url that begins with "jdbc:mariadb:" or "jdbc:mysql:".

Assuming a server is installed on the local machine with default port 3306, the url String is then "jdbc:mariadb://localhost/".

Basic maven archetype has created a simple Java file in src/main/java/com/mycompany/app. Update the file with the following content: (assuming a server is installed on the local machine, with a user "root" with no password) :


import java.sql.*;

public class App {

    public static void main( String[] args ) throws SQLException {
        //create connection for a server installed in localhost, with a user "root" with no password
        try (Connection conn = DriverManager.getConnection("jdbc:mariadb://localhost/", "root", null)) {
            // create a Statement
            try (Statement stmt = conn.createStatement()) {
                //execute query
                try (ResultSet rs = stmt.executeQuery("SELECT 'Hello World!'")) {
                    //position result to first
                    System.out.println(rs.getString(1)); //result is "Hello World!"

Compile project:

mvn install

Maven will automatically download the driver and compile the App file.

Run it using maven:

C:\temp\my-app>mvn exec:java -Dexec.mainClass=""
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Building my-app 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ my-app ---
Hello World!
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.837 s
[INFO] Finished at: 2017-10-25T11:16:06+02:00
[INFO] Final Memory: 10M/245M
[INFO] ------------------------------------------------------------------------

See Also


Comments loading...
Content reproduced on this site is the property of its respective owners, and this content is not reviewed in advance by MariaDB. The views, information and opinions expressed by this content do not necessarily represent those of MariaDB or any other party.