Have you ever figure out an idea for your own project and you finished building your UI but you just don’t know how to start up your own backend server?
Reading this article I assume you have mastered basics of core java before moving forward.
I will talk about how to get a spring jdbc project running on a pc. And this article will be talking about how to install all the required components onto your machine.
Before getting the project setup, let’s understand what jdbc is.
It stands for Java Database connectivity, which is a Java API used to establish connection between the java language and different variety of databases, such as mysql, postgres,etc.
With spring boot, we can establish a running server that takes in request from client code, performing crud operations to update our database and getting those data back to the client.
To start off, let’s list out all the steps we need to take to get this project setup:
- downloading intellij ide
- setup project
- set up libraries and configuration settings
- set up database from scratch
- setting up directories for the server code
Step 1: Download intellij
Go to Download IntelliJ IDEA: The Capable & Ergonomic Java IDE by JetBrains and download the community exe if you are on a pc. They also have versions for mac as well. If you are willing to pay, you can get intellij ultimate as well.
Step 2: Download Java
Go to Java SE — Downloads | Oracle Technology Network | Oracle and download a java package. Click on “JDK download” and it will download an exe file. Follow the steps properly and it will install the java library onto your machine. It can be any version but I personally use Java 8 since it’s all we need to support a simple jdbc project.
Step 3: Install postgres onto your machine.
Technically, you can use any relational database to complete this part. In my case I use postgres as the database to connect to. Go to PostgreSQL: Downloads and you can download the postgres database down.
If this step might not be clear, read this tutorial to install postgres with further clarification: Install PostgreSQL (postgresqltutorial.com).
P.S: set password as “password” just to be consist with the rest of the article.
Step 4: install pgAdmin
In this step, you will be downloading a UI application that will help you manage your database. I personally use pgAdmin but you are feel free to use anything else if you want to.
Once you install it, open the application on your pc and it should prompt you for your password. Once you login successfully, you should be able to see your database:
For now, we are not going to do much but eventually we need to use pgAdmin to validate the
Step 5: Go to spring initializer io and set up your spring boot project.
Technically, you can set it up on your own once you finish setting up intellij but spring initializer help us import a lot of the files & settings automatically.
Go to Spring Initializr and you should be able to see this page:
Now Chose maven project as your project option. Choose “Java” as your language option. You can choose any spring boot version but I just kept the same version as default.
Afterward, you can set project metadata, which contains description details of your project. You can leave it as it is or you can modify according to the functionality of your application.
Now set the packaging to a jar file and set java version to java 8. You can choose any java version you want but make sure the version matches the java library you installed onto this machine.
Click on “Add Dependencies” and in the search bar type in “PostgresSQL Driver” and then press enter.
Afterward, click on “Add Dependencies” and in the search bar type in “Spring Data JDBC” and select it.
Now click “generate” and it will generate a zip file. You can decompress it and it will be a folder consist of the project itself. For convenience, you can locate this folder under the C drive or D drive. Just create a project folder and put the project into the folder.
Step 6: Open intellij and set up additional dependencies in the pom.xml
Open intellij idea in your computer and it should prompt you to create a new project or open one. Click “open” and locate the folder of your project. Afterward, your project should look like this:
Now let’s set up additional dependencies in your pom.xml file.
Yes, I know. We should have included all the needed dependencies on step 3 but this is to ensure the project runs properly by containing all the important packages. Now locate your pom.xml and ensure it looks something similar to the file provided below:
Here, we have spring boot starter jdbc, spring boot starter web and spring boot starter as dependencies. All of these are important configuration settings for establishing a maven based spring boot jdbc project.
Of course, we also got the postgres dependency, which tells spring boot what kind of database we will be using.
Notice there is another package call lombok. This package is optional and is a java library that allows you to skip the step of writing getters and setters later on when you are setting up the entity object that we will map our database data to.
Step 7: Setup you database settings in your application.properties file
Locate the application.properties file under the resources path and place with the following contents:
If you installed postgres on the database, the postgres will always set to port 5432 by default. For our username it will default as postgres and the password will be “password”, which is set when you install postgres onto your machine.
Now you can go to DemoApplication.java under src/main/java/com.example/demo and click on the green play icon on line 9:
and your application should run on port 8080!
That’s it for the first part of this tutorial. I will cover the crud operations and what we need to know to fully set up this project.
For the second part, click on this link to finish the full tutorial: Setting your own spring boot server via JDBC part 2 | by Michael Tong | Jan, 2021 | Medium