From the course: Advanced Kotlin Database Development

Connecting to your database

- [Instructor] To start working with any database we need to connect to it first. Let's understand what information we need to establish a connection. Then we'll see how to do that in practice. In order to connect to a database, we need to know six things about it. First, we need to know the database vendor. Then we need to know where it's located, it's host and port. We also need to know database name and to be able to authenticate, we need a username and password. Now let's discuss each of those parameters in more detail. We'll start with database dialects. Exposed library currently supports seven popular databases. Those databases are developed by different companies, so in order to connect to any of those databases we need to have a driver library in our project. We will see later how we add this library. But for now, let's just say that in this course, we'll focus on PostgreS database since it's an open source database that is very popular nowadays. If you want to practice those exercises using another database, for example MySQL, then you'll need to replace the driver library with the correct driver for your database. A host is the machine that database runs on. If it runs on the same machine as our server, we usually would say it runs on local host. This will be the case for this course. In production systems, you would have the database running on a separate server from your services. So in order to connect to it, you'll need to know what is the DNS or the IP of the machine it runs on. Some databases that Exposed framework supports are embedded databases. Those databases only run locally. SQLite database is popular for mobile development and H2 database is often used for tests. Every database also has a default port except for embedded databases such as SQLite and H2. Here I listed some of the default ports. For some database types, a single database server can support multiple databases. For example, with PostgreS, you have the default database that is called PostgreS that has some metadata, but for your application, you would usually create a separate database. Finally, you need a username and password to connect to a database. Sometimes you can use a default user or leave the password empty, but it's good security practice to use a user that was created for your application and to have a password. So that's what we will do. That concludes the theory part of this section. So now let's jump into coding. So here we have our generated project, which has a single class called Main that prints Hello Exposed when I run it. First thing we need to do is to add the Exposed library to our project. This is project managed by Gradle. So we'll go into the build Gradle file and add a new dependency to the dependencies block. The second dependency we need to add is the driver for our specific database. As I mentioned earlier, each database has its own driver and each version of the database needs the correct version of that driver. I'm using PostgreS14, which is the latest stable PostgreS version at the moment. So I'll pick the most current version for this driver. Now let's build this project. Well, that's it for the dependencies. Let's go to our main file and add a new line that will establish a connection to the database. In order to do this, I will use the database object that we have from Exposed and it has a connect method. I will need to import that object and now I will need to specify the type of the database, so it will be PostgreS. The host, the port, and the name of the database. Then I will specify the user. And finally, the password. One important note here is that this line of code sets the connection to the database but it doesn't connect to the database. The connection will happen only when we open our first transaction and that's something we will learn in one of the next videos. That means that even if our database is not running, this code will still execute just fine. But let's see that it at least compiles.

Contents