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:

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:


GeSHi Error: GeSHi could not find the language jsp (using path /nas/content/live/orkbprod/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

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:

@RequestMapping("/delete")
public String deleteCars(@RequestParam Integer id) {
service.delete(id);

return "redirect:/";
}

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:
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 the String 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 fields carname, brand and remarks.

  • After that, we can update our service class using the following code:

public List<Cars> search(String keyword){ return
carsRepo.search(keyword);
}
  • In the application’s controller class, we’ll implement a method using the code shown below:
@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:


GeSHi Error: GeSHi could not find the language jsp (using path /nas/content/live/orkbprod/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

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:

Gif shows that the search function is working using the keyword as it's criteria

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

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.