Java Connector Using Gradle

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

Prerequisites

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

Create Gradle Project

Create a simple Java project with gradle :

gradle init --type java-library

The new project will be created in current folder. This folder contains the file build.gradle that permits configuring Gradle.

Get MariaDB Java Driver

Declares driver in build.gradle (and setting java minimal version to 1.8) :

The build.gradle file will then be :

// Apply the java-library plugin to add support for Java Library
apply plugin: 'java-library'

// In this section you declare where to find the dependencies of your project
repositories {
    // Use jcenter for resolving your dependencies.
    // You can declare any Maven/Ivy/file repository here.
    jcenter()
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    // This dependency is exported to consumers, that is to say found on their compile classpath.
    api 'org.apache.commons:commons-math3:3.6.1'

    // This dependency is used internally, and not exposed to consumers on their own compile classpath.
    implementation 'com.google.guava:guava:22.0'

    // Use JUnit test framework
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
	
    implementation 'org.mariadb.jdbc:mariadb-java-client:3.4.1'
	
}

Connection

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/".

Create a new file App.java in src/main/java 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
                    rs.first();
                    System.out.println(rs.getString(1)); //result is "Hello World!"
                }
            }
        }
    }
}

To run class App, add a new task in build.gradle:

task run (type: JavaExec){
    description = "get started run"
    main = 'App'
    classpath = sourceSets.main.runtimeClasspath
}

build project:

c:\temp\gradle>gradle build

BUILD SUCCESSFUL in 1s
4 actionable tasks: 4 up-to-date

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

To run the App class, just launch the previously-defined task "run":

c:\temp\gradle>gradle run

> Task :run
Hello World!


BUILD SUCCESSFUL in 1s
2 actionable tasks: 1 executed, 1 up-to-date

See Also

Comments

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.