Java and PostgreSQL Web Application Part 9
Introduction
Building a Java web application that can interact with a PostgreSQL database is a complex task that involves a number of steps. Throughout our tutorial series, we’ve been providing step-by-step instructions for building an example application. In this last installment of the series, we’ll show you how to create the delete and search features of the application– the final steps in building the app.
Prerequisites
Before you proceed with the steps outlined in this article, make sure that you’ve read and completed the steps from the previous articles in the series:
- Java and PostgreSQL web Application PART 1
- Java and PostgreSQL web Application PART 2
- Java and PostgreSQL web Application PART 3
- Java and PostgreSQL web Application PART 4
- Java and PostgreSQL web Application PART 5
- Java and PostgreSQL web Application PART 6
- Java and PostgreSQL web Application PART 7
- Java and PostgreSQL web Application PART 8
Coding the Java and PostgreSQL Web Application’s Feature
In the previous article, we wrote the code for the “edit record” and “update record” features of our Java and PostgreSQL web application. In this article, we’ll build out a few other features of the web app such as the search and delete features.
Deleting Record in Java and PostgreSQL Web App
Let’s start by working on the delete feature of our web application.
First, we’ll update our index.jsp file by adding the code shown below. This code will invoke the delete function:
This will be the second action that we define as discussed in Java and PostgreSQL web Application Part 1, the first article in this multi-part series.
After adding the above code to our index.jsp, we’ll create a custom handler in our controller class:
1 2 3 4 5 6 |
The code shown above simply performs a delete function against the database. It identifies the record to delete based on the id that was captured by the @RequestParam annotation.
After deleting the record, the handler will then redirect the user to the homepage using the code return "redirect:/";
.
Coding the Search Feature of the Java and PostgreSQL Web Application
We’ve reached the final stretch in the construction of our web application. In this section, we’re going to write the code that will perform a search function via keyword. Our search functionality will allow the user to search on three fields: carname
, brand
and review
.
We’ll need to perform the following steps to create our search functionality:
- First, we’ll update the Java interface
CarsRepository
with the following code:
1 2 3 4 5 6 7 | public interface CarsRepository extends CrudRepository<Cars, Integer>{ @Query(value="SELECT t FROM Cars t WHERE t.carname LIKE '%' || :keyword || '%'" + " OR t.brand LIKE '%' || :keyword || '%'" + " OR t.remarks LIKE '%' || :keyword || '%'") public List<Cars> search(@Param("keyword") String keyword); } |
We use the
@Param
annotation to specify the parameter for the query; in this case, the parameter is the word “keyword”, which wraps theString keyword
. The beauty of this code is that we don’t actually need to write any real code. We simply specify a query using the@Query
annotation.Using the JPQL query format, we leverage the
LIKE
operator and ‘%’ sign to perform a multi-character search against the fieldscarname
,brand
andremarks
.After that, we can update our service class using the following code:
1 2 3 |
- In the application’s controller class, we’ll implement a method using the code shown below:
1 2 3 4 5 6 7 8 | @RequestMapping("/search") public ModelAndView search(@RequestParam String keyword) { ModelAndView modelView = new ModelAndView("search"); List<Cars> result = service.search(keyword); modelView.addObject("result",result); return modelView; } |
We’ve seen code with a similar format multiple times throughout this tutorial, so we’ll skip any discussion of the details. This code simply presents the user with a page named “search”, which will be populated with data via the application’s model class.
To be able to see the result, we’ll need to create the JSP file and name it “search.jsp”. We can use the following code for this JSP file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="t" %> <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <meta charset="ISO-8859-1"> <title>Search Result</title> </head> <body> <div align="center"> <h1>Cars Search Result</h1> <table border ="1" cellpadding="5"> <tr> <th>ID</th> <th>Car Name</th> <th>Car Brand</th> <th>Review</th> </tr> <t:forEach items="${result}" var ="cars"> <tr> <td>${cars.id}</td> <td>${cars.carname}</td> <td>${cars.brand}</td> <td>${cars.review}</td> </tr> </t:forEach> <tr> <td colspan="5"><a href="cancel" class="badge badge-dark">Go Back</a></td> </tr> </table> </div> </body> </html> |
The above code is fairly self-explanatory– it simply displays the record based on the criteria set in the search query.
After we save the file, we can test our application by running the server and navigating to our application in a browser.
In the search bar, let’s try using the keyword “toy”, and we’ll see what it returns.
Here’s what the output will look like:
We see that our web application returns two records from our database– our project was a success.
Conclusion
If you’ve been following along with this entire tutorial series, you know that this PostgreSQL and Java web application was a complex project with quite a few steps. This article concluded the series with step-by-step instructions for coding the delete and search feature of our web application. With this tutorial to guide you, you’ll be able to build your own Java web app from start to finish.
Pilot the ObjectRocket Platform Free!
Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.
Get Started