Introduction to MariaDB Java Connector
MariaDB Java connector is the library that is licensed under LGPL and is used for establishing the connection between the applications developed by using Java programming language to the Database Management systems such as MariaDB and MySQL. This library is a driver of Type 4 for JDBC. MariaDB java connector is a lightweight driver that was developed specifically for databases such as MariaDB and MySQL.
What is MariaDB Java Connector?
It is used for establishing the connection between the java applications to MariaDB or MySQL. The original root code for this connector was taken from the drizzle JDBC library. Further, many changes were made and the bugs were fixed by changing the code which led to formation of MariaDB Java connector. This connector is suitable and compatible for all the MySQL and MariaDB servers having their version 5.5.3 and above.
The version of MariaDB Connector that are previous to 1.2.0 can even work with an older version of MySQL such as prior to 5.5 and old version of MariaDB prior to 5.5. For the current versions, this old version of MariaDB 1.2.0 and prior to it are not supported anymore. In order to cross-check which version of connector which suit to which version of java language used in applications, you can refer to the below table –
|Version of MariaDB connector||Corresponding supporting version of Java|
|2.0 and above||Java 8 and Java 11|
|Version ranging from 1.6 to 1.8||Java 7, Java 8, and Java 11|
How do I set up MariaDB java connector?
We can download and install the java MariaDB connector by using the package managers such as Gradle or Maven. Other than this manual installation can also be done by downloading the jar file and setting the same in our class path. For a complete reference, you can go to the official link https://mariadb.com/kb/en/installing-mariadb-connectorj/.
Maven installation –
For Maven dependency addition, you can simply do some changes in pom.xml file of your project and add a dependency tag for group id with org.MariaDB.JDBC and artificat if with MariaDB-java-client as shown below –
<dependency> <groupId>org.mariadb.jdbc</groupId> <artifactId>mariadb-java-client</artifactId> <version>required version </version> </dependency>
The required version should be the version of MariaDB Java connector you wish to install.
Installation of Java MariaDB connector using Gradle package manager –
You will have to add the following line in the configuration file of build.gradle for installing connector.
Required_version should be any of the versions which you want to install of the java MariaDB connector.
Installing using jar file –
You can install the Java MariaDB connector manually by downloading the jar file from the link https://downloads.mariadb.org/connector-java/ and then setting the jar file to the class path of your directory.
How to Connect MariaDB java connector?
You can connect by using either of two available ways is using Driver manager or pool –
Using the DriverManager class is the most preferred way as no extra configurations are required other than to find, locate and load. We can use this connector in the same way as that of the JDBC connector once the loading of the connector is automatically done by the Driver Manager. We can make the use of the following sample statement to establish the connection using
DriverManager class –
Connection objOfConn = DriverManager.getConnection("jdbc:mariadb://126.96.36.199:3306/EducbaDatabase?user=payal&password=samplePass");
Another way of establishing the connection is by using the connection pool where we can make the use of either of two available pools that are listed here –
- MariaDbPoolDataSource – This pool involves using the implementation of a connection pool where a pool or group of connections is maintained and if any of the new requests of connection comes in then one of the connections from the pool is borrowed.
- MariaDbDataSource – This is a very simple and basic implementation of a connection pool where each time we give a call to getConnection() method a new connection is created.
The structure and syntax of the connection string used in JDBC for Java MariaDB connector is as shown below –
jdbc:(mariadb|mysql):[replication:|loadbalance:|sequential:|aurora:]//<details of host>[,<details of host>...]/[name of database to connect][?<name of key1>=<corresponding value1>[&<name of key2>=<corresponding value2>]]
The details of host involve mentioning of host name, port address and type of the host which can be either slave or master. The sample example of the same is as shown below –
• address = (host = 188.8.131.52) (port=3306) (type=slave)
Preparing MariaDB java connector
It makes use of prepared statements as the structure to have communication with the database. Text protocol will be used by the driver only if the options of rewriteBatchedStatements and allowMultiQueries are having the value set to true. The parameter substitution of the prepared statement is carried out on the client-side.
The following steps need to follow in order to prepare on the Windows platform –
- The environment variables in the classpath should have the proper location of the connector set to it. Note that the complete location of the jar file involving all its directory structures also called a fully qualified name should be set. For example, if your jar file is located in c drive C:\User\Educba\MariaDB\MariaDBJavaConnector/MariaDB-java-client-1.2.2.jar then you can make the use of following command in the command line –
CLASSPATH = C:\User\Educba\MariaDB\MariaDBJavaConnector/mariadb-java-client-1.2.2.jar
- The JAVA_HOME and JDK_HOME Java run time environment variables should be set to the proper location. For example, if the location of the java runtime environment is C\ User\Educba\java\jre7 then you can make the use of the following command –
JAVA_HOME = C\ User\Educba\java\jre7
- The next step involves restarting your MySQL or MariaDB database server.
For the UNIX platform, you can simply follow the same steps of setting the class path to driver location, changing the environment variables for JDK. Along with that, you also need to specify the location of the installation directory of JCM Java Virtual Machine in the environment variable named $LD_LIBRARY_PATH and then restarting the database server
MariaDB Java Connector is the library of driver that can be used for establishing the connection between the MariaDB Database and Application written in Java programming languages. Be sure about which version will be suitable according to your requirements and then proceed with the installation process as mentioned above.
This is a guide to MariaDB Java Connector. Here we discuss the Introduction, What is MariaDB Java Connector? examples with code implementation. You may also have a look at the following articles to learn more –