Java connector using graddle
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
- graddle
Create gradle project
Create a simple Java project with graddle :
gradle init --type java-library
The new project will be created in current folder. This folder contains the file build.gradle that permit to configure Gradle.
Get MariaDB Java Driver
Declares driver in build.gradle (and setting java minimal version to 1.8) :
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 'junit:junit:4.12' implementation 'org.mariadb.jdbc:mariadb-java-client:2.1.2' }
Connection
Standard JDBC methods DriverManager.getConnection(String url, String user, String password) is used to connect database.
Basic url string is standardized for MariaDB driver :
jdbc:(mysql|mariadb):[replication:|failover:|sequential:|aurora:]//<hostDescription>[,<hostDescription>...]/[database][?<key1>=<value1>[&<key2>=<value2>]]
MariaDB driver registered automatically for url that begins with "jdbc:mariadb:" or "jdbc:mysql:".
Assuming a server installed on the local machine with default port 3306, url String is then "jdbc:mariadb://localhost/"
.
Create a new file App.java in src/main/java with the following content: (assuming a server 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 in build.gradle a new task
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 App file.
To run App class, just launch the task "run" defined previously:
c:\temp\gradle>gradle run > Task :run Hello World! BUILD SUCCESSFUL in 1s 2 actionable tasks: 1 executed, 1 up-to-date
more information on about-mariadb-connector-j