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.
A MariaDB/MySQL server running on localhost using the default port 3306
Create Gradle Project
Create a simple Java project with Gradle:
The new project will be created in the 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 :
Standard JDBC methods, , are used to connect to the database.
The 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):
To run the class app, add a new task in the build. Gradle:
Build project:
Gradle will automatically download the driver and compile the app file. To run the App class, just launch the previously defined task "run":