This content originally appeared on Level Up Coding - Medium and was authored by Nilanjan
Learn how databases are accessed at the low-level using JDBC

Accessing databases is a common task. All the server-side applications access databases in one way or another. As long as Kotlin is concerned it inherits a rich set of libraries from the Java world, thanks to the interoperability. These libraries provide dazzling features like ORM, typesafe SQL, and so on.
However, if you are doing this for the first time, all the sophisticated libraries may not make much sense. To get you up and running, in this article, I will give you a quick introduction to JDBC.
JDBC stands for Java Database Connectivity. It’s a low-level library that provides functionality to interact with a database.
A Quick Note!
I would suggest that you should read the following articles that I linked below. The first article provides a well-round introduction to JDBC. And the second article gives a comprehensive overview of all the libraries and frameworks of the Java world that are available at your disposal.
However, these two articles are based on Java. So, if you are not familiar with Java the contents may be hard to digest. In that case, just try to skim through them and proceed to the next section of this article. And if you are familiar with Java then you need not come back and read this article. It would feel redundant.
- JDBC — a short guide by Macro Behler
- Java & Databases: An Overview of Libraries & APIs by Macro Behler
Here is the TL;DR of the first article:
- You can access a database through the low-level JDBC library. It comes bundled with all the JDKs out there. So, no need for another dependency.
- To connect with a particular database you need the JDBC driver for that particular database.
- All libraries for databases in the JVM world are built on top of JDBC.
- While working on a real project you should be using a connection pooling mechanism. Connection pooling lets you keep a small number of database connections alive. Instead of creating a fresh connection every time, you (re)use a connection from the pool.
HikariCP is a good choice while implementing connection pools. By default, it uses 10 connections as the maximum limit. And it is easily modifiable.
A simple demonstration of accessing a database using low-level JDBC
The code used in this article is available in this Github repo.
Let’s create a table called users and populate it with some data. I will be using postgresql. You can use the database of your choice.



To access this data from your Kotlin code, you have to include the JDBC driver of your database as a gradle dependency. If you are not using postgresql , find the driver for your database.

Now, we’ll see how to retrieve the data from the database.
First, create a model class for the data we are about to receive from the database.

To create a connection to the database with JDBC you would need a JDBC url . Here is how the JDBC url looks like,

As you can see the JDBC url consists of many parts. The host name , the port number and the database name .

To create a connection, use the DriverManager.getConnection method.

The first argument of the DriverManager.getConnection method is the JDBC url , the second argument is the username of the database and the last argument is the password for that particular user.
You can verify whether the connection is valid or not using the isValid() method on a Connection object.

Now, you can write and execute SQL queries using the connection. But as JDBC is a low-level library everything needs to be done by hand. Consider the following example,

Why do we need additional abstraction over JDBC?
As you can see, accessing databases with JDBC can quickly become a cumbersome task. The process is error-prone and a lot of heavy lifting and discipline is required from the developer-end. This is where the libraries and frameworks come in handy.
When it comes to libraries and frameworks to access databases there are numerous flavors to choose from. You may opt to use a full-fledged ORM like Hibernate, or you can use something low-level like JDBI . JDBI provides a convenient layer of abstraction over JDBC. There are a few Kotlin-first libraries as well. The most popular is the Exposed library from Jetbrains. Exposed comes in two flavors. A typesafe SQL wrapping DSL and lightweight data access objects.
Wrapping Up
Irrespective of the library you decide to go with, now you have a basic understanding of what’s going on under the hood while you are querying your database from the application layer.
I am glad that you have finished reading the article. I would really appreciate a couple of words on your thought. ✏️
JDBC: How to Access Databases with Kotlin was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Nilanjan

Nilanjan | Sciencx (2021-11-09T02:33:59+00:00) JDBC: How to Access Databases with Kotlin. Retrieved from https://www.scien.cx/2021/11/09/jdbc-how-to-access-databases-with-kotlin/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.