Connecting a Java Backend to a Postgres Database

INITIALIZE A SPRINGBOOT PROJECT

The first step is to head over to the Springboot Initializer. You can create a project with the default settings but under “dependencies”, be sure to add Spring Web, Spring Data JPA and the PostgreSQL driver as such:

IMPLEMENTING API

As you will see by navigating to src/main/java/com/example/demo/DemoApplication.java, the default code we are given looks like this:

package com.example.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
public string working() {
return "It's working!";
}

CREATING OUR CLASS

First step to creating our class it to make a directory within our “demo” folder. For this tutorial I want to make an “exotic birds” class, so feel free to replicate or create something you would like to see. I made a folder titled “bird” with a Birds.java file inside. Beneath the import statement we will want to initialize our class with name, family (genus) and the year they were formally introduced to the animal kingdom in the realm of our awareness. We will want to follow that up with getters and setters for each parameter then convert them to strings. Here is the code I have written for this example:

package com.example.demo.bird;public class Bird {
private Long id;
private String name;
private String family;
private Integer yearIntroduced;
public Bird() {

}
public Bird(Long id,
String name,
String family,
Integer yearIntroduced) {
this.id = id;
this.name = name;
this.family = family;
this.yearIntroduced = yearIntroduced;
}
public Bird(String name,
String family,
Integer yearIntroduced) {
this.name = name;
this.family = family;
this.yearIntroduced = yearIntroduced;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

public String getFamily() {
return family;
}
public void setFamily(String family) {
this.family = family;
}
public Integer getYearIntroduced() {
return yearIntroduced;
}
public void setYearIntroduced(Integer yearIntroduced) {
this.yearIntroduced = yearIntroduced;
}
@Override
public String toString() {
return "Bird{" +
"id=" + id +
"name=" + name +
"family=" + family +
"yearIntroduced=" + yearIntroduced +
'}';
}
}
public List<Bird> getBird() {
return List.of(
new Bird(
1L,
"Flamingo",
"Phoenicopterus",
1831
)
);
}

CREATING A BIRD CONTROLLER & SERVICE LAYER

Next, we will want to create a file within our bird directory called BirdController.java. This controller will require RestController, RequestMapping, GetMapping and Autowired annotations, and with a path passed to the RequestMapping of “api/v1/birds” so that will be the new endpoint tacked on to our server. The service layer will contain the instance we created in the DemoApplication for more efficient management, and will make use of the “@Service” annotation. My code for this portion looks like this in DemoApplication:

package com.example.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
package com.example.demo.bird;import java.util.List;public class BirdService {
public List<Bird> getBirds() {
return List.of(
new Bird(
1L,
"Flamingo",
"Phoenicopterus",
1831
)
);
}
}
package com.example.demo.bird;import java.util.List;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping(path = "api/v1/bird")
public class BirdController {
private final BirdService birdService;@Autowiredpublic BirdController(BirdService birdService) {
this.birdService = birdService;
}
@GetMapping
public List<Bird> getBirds() {
return birdService.getBirds();
}
}

CREATING AND CONNECTING TO A DATABASE WITH JPA

To initialize this procedure we will want to head over to the application.properties file within the src/main/resources directory. To configure this for Postgres access, we will want to add this code to the file:

spring.datasource.url=jdbc:postgresql://localhost:5432/bird
spring.datasource.username=
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql=true

--

--

--

Programmer / Artist / Believer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The End of Product Development Roadmaps has come — finally!

Fun with Marketo — Building a Marketo integration in 15 minutes

eBPF: Dive into the verifier!

Storing your JWTs in HTML Web Storage is (maybe) a bad idea

Insights: ISO 20022 Business Application Header (BAH)

Brief introduction to CSS-Grid

My First Contribution To Open-Source (Mozilla)

Logo of Mozilla

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Lucas Thinnes

Lucas Thinnes

Programmer / Artist / Believer

More from Medium

Configuring JSP in Spring Boot Application

Java 12 to 17 — All You Need To Know

JAVA KEYSTORE API

Spring Boot Test