Spring Form Submission to PostgreSQL – Spring Boot

Spring Form Submission to PostgreSQL – Spring Boot

The tutorial will introduce you how to submit a web form to PostgreSQL database.

I. Technology for Spring Form Submission to PostgreSQL

– Java 1.8
– Maven: 3.3.9
– Editor: Spring Tool Suite – Version 3.7.3.RELEASE
– Spring Boot: Version: 3.8.0.RELEASE

II. Overview
1. Struct of Project

spring form submission to postgresql structure

2. Step to do

– Create Spring Boot project
– Configure datasource
– Create a Data Model
– Create a Repository
– Create a Web Controller
– Create web views
– Create database table
– Run & Enjoy result

3. Demo Video
III. Practices
1. Create Spring Boot project

Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, input info as below picture:

spring form submission to postgresql -  createproject

Press Next button. Now add needed dependencies:

– For Spring Web MVC, choose Web then select Web
– For Thymeleaf view technologies, choose Template Engines then select Thymeleaf
– For JPA and Postgresql, choose SQL then select JPA and PostgresSQL.
Follow images:

spring form submission to postgresql -  createproject - add dependencies

Then Press Finish. Spring boot project is created successful.
Open pom.xml for check dependencies:

2. Configure datasource

Open application.properties, configure datasource:

3. Create a Data Model

Create a simple data model: Customer.java

@Entity:Specifies that the class is an entity. This annotation is applied to the
entity class.
@Table:Specifies the primary table for the annotated entity.
@Id:Specifies the primary key of an entity.
@Column:Is used to specify the mapped column for a persistent property or field.

4. Create a Repository

interface CrudRepository: Interface for generic CRUD operations on a repository for a specific type.

5. Create a Web Controller

Create SimpleWebController with 3 @RequestMapping
– @RequestMapping(value=”/form”, method=RequestMethod.GET): get input form
– @RequestMapping(value=”/form”, method=RequestMethod.POST): submit input form to postgresql
– @RequestMapping(value=”/load”, method=RequestMethod.GET): load customer from postgresql then shows on web.

6. Create web views

For each @RequestMapping, we create a html view:
form.html: create a form for input customer info.

result.html: create a notified view after submit customer form.

load.html: create a view that displays info of a customer form database.

7. Create database table

Open pgAmin III , and create customer table as below sql:

spring form submission to postgresql-form customer - sql

Now customer table is created succesfully.

spring form submission to postgresql-form customer - create table successful

8. Run & Enjoy result

Maven Build :
clean install

Maven Run as mode: Spring Boot App
Make request:
http://localhost:8080/form

Input info, then press Submit:

spring form submission to postgresql-form customer

Result

– On Web:
spring form submission to postgresql -  submit successful

– Check Database:

spring form submission to postgresql-form customer - table

Request:
http://localhost:8080/load?id=10

Result:

spring form submission to postgresql -  load result

IV. Source code

springformhandler


Related Posts


3 thoughts on “Spring Form Submission to PostgreSQL – Spring Boot”

  1. hi, this is eli, all the way from Kenya. wanna thank you for this great tutorial. i used it to build on my knowledge about linking my app with the db. i have tried with different databases and it works perfectly.
    thanks alot.
    keep them comming

    1. Hi Eli,

      Thank you so much for your feedback 🙂
      We’re on the way to make this Site more useful for all of you.

      Best Regard!

      1. hi, i need some help with making updates to my table entries.
        (both back-end and front-end) of the application, with refence to this tutorial.
        Any help is appreciated.
        Regards.

Got Something To Say:

Your email address will not be published. Required fields are marked *

*