Introduction
You might have written many java programs which just do some logical computation. Have you ever wondered to store the value that your program computes so that you can play along with it later on or just storing the value for your record purpose which you can use and manipulate it? So in this article, I will share how you can connect the MariaDB database with your java program in Ubuntu 20.04 LTS. You can use the same method to connect to the lower version of ubuntu like (19.10, 18.04, etc...).
MariaDB JDBC
In order to connect the java program with the MariaDB database, you need to have Java Database Connector(JDBC). JDBC is the standard Java API for database-independent connectivity between the java program and the various databases. So with the MariaDB database, we will make use of Connector/J JDBC. You can download it from MariaDB.com[Click Me]. Based on your Java version you can select and download it. For java version 8 and above you can download the Connector/J with java version 8. It will work.
Installation of MariaDB
In case if you have not installed the MariaDB database you can make use of the following commands.
sudo apt-get update
sudo apt-get install mariadb-server
This will install the MariaDB in your ubuntu operating system. In case if the MariaDB doesn't start then you can make use of the following commands to check the status and start it.
sudo systemctl status mariadb
sudo systemctl start mariadb
Once the MariaDB server is running you have to configure it so that you can use it. By default, there will be a root user. So to configure the root user password use the following commands which will guide you to set the password for the root user.
sudo mysql_secure_installation
It will give you a series of options to complete it. Just read the instruction and follow them. Once it is completed you can log in to the MariaDB server using the following command.
sudo mysql -u root -p
Here the root is the username and the parameter 'p' stands for the password. You have to give a password if you give a 'p' parameter.
For the demonstration purpose, I will make the database called 'test' and it will have a table called 'user' with name, age, and gender as its attributes/column.
Create Database, Table and Grant Permission to the User
Once you are login into the MariaDB you can use the show databases command to see the available databases.
show databases;
To create and select the database you can make use of the following commands.
create database test;
use test;
Now to create the user table with the name, age, and gender attributes you can make use of the following command.
create table user(name varchar(25), age int(3), gender varchar(6));
To display the available table in the database you can make use of the following command.
show tables;
Now to display all the table attributes of a specific table you can make use of the following command.
describe user;
In order to verify that our database connection is correct, let's enter some dummy data so that we can access it through our java program. Use the following commands to insert the data into the user table.
insert into user(name, age, gender) values("Pema Dorji", 23, "Male");
insert into user(name, age, gender) values("Pema Wangmo", 21, "Female");
If you want to verify from the database shell itself you can make use of the following commands.
select * from user;
It will display all the records available in the user table. Now the last thing to do is that grant all the permission to the user root for the test database. You can make use of the following command.
grant all privileges on test.* to "root"@"localhost" identified by "password";
flush privileges;
Here the 'root' is the username and the "password' should be the password of the user root.
Java Program to Connect and Access MariaDB Database
Now finally we will write the code to connect to the MariaDB database with the java program. You can get some examples of how to use the Connector/J JDBC from the dev.mysql.com[Click Me].
You can also access the source code from my GitHub page at ConnectionDemo.java[Click Me].
How to run the above Jave Program Code
Once you have written the java program code, it is time to compile and run the java program code. Before we compile it to make sure that your java code and the Connector/J JDBC are in the same directory. Once it is kept in the same place then you can make use of the following command to compile it.
javac -cp name_of_Connector.jar: java_program_file.java
In order to run it, you can make use of the following command.
java -cp name_of_Connector.jar: java_program_file
Note: The name_of_connector.jar should be replaced with the JDBC connector jar file name. The java_program_file.java should be replaced with the java code file(Eg. hello.java). When you run the program the java_program_file should be replaced with the name of class file(Eg.Hello.class) without .class extension.
The output of the Above Java Program Code
Conclusion
You have learned how to connect the MariaDB database using the Connector/J JDBC. You have also learned how to compile the java code with an external jar file and run it. If you have any queries feel free to drop a comment in the comment box.