Java and PostgreSQL Web Application PART 8

Introduction

This is Part 8 of the multiple-series tutorial that explains how to build a Java PostgreSQL web application from scratch with ECLIPSE, Spring Data, Tomcat, and other databases. Part 7 showed you how to add handlers for creating and inserting records into a database, and then view them on your web application’s home screen. Today, you’ll advance to adding more features to your web application.

Prerequisites

Coding the Java and PostgreSQL Web Application’s Feature

Specifically in this lesson, Part 8, you’ll add the update/edit functionality to your Java PostgreSQL web application.

Updating/Editing Record in Java and PostgreSQL Web Application

The image below shows what you can expect your application to resemble after you’ve added the update/edit component.

image shows the editC_car.jsp page

The page called “Edit Car Details” is recognized by the index file’s parameter.

  • To perform a record edit, access your file index.jsp, and then use this code to update the file:

GeSHi Error: GeSHi could not find the language jsp (using path /nas/content/live/orkbprod/wp-content/plugins/codecolorer/lib/geshi/) (code 2)
  • Next, in the controller class, make a custom handler with this code:
@RequestMapping("/edit")

public ModelAndView editCarForm(@RequestParam Integer id) {

ModelAndView modelView = new ModelAndView("edit_car");

Cars cars = service.get(id);

modelView.addObject("cars", cars);

return modelView;

}

Here’s an explanation of the code you just made:

  • An object ModelAndView is returned when you used the annotation @RequestMapping(“/edit”).

  • You added the annotation @RequestParam and the data type specified to get the passed parameter. (The code ?id={cars.id} was used to obtain the parameter.)

  • The views folder holds the “edit_car” view. That view name was assigned to the modelView, the ModelAndView object.

  • To get the function service.get(id), you performed a Cars object instantiation.

  • Lastly, you went back to modelView.

Now that the handler is composed, it’s time to make the view from the _editcar.jsp class controller with this code:


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

The code above should look familiar. That’s because it resembles the same code in the file _createcar.jsp you created previously with the exception of this supplementary code here:


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

It’s an important piece of code where form:hidden path="id" determines the specific records to update based on the form’s values.

  • Restart your web application to try out the new edit/update feature. To do this, in a browser, input the http://localhost:8080/CarInformationSystem/ URL.

  • Next, select a record with the number “2” as the ID, and then press the button Edit button.

  • The result should look something like this:

image show the edit record feature

NOTE: When you add a feature using code, clicking Save will activate the function to save, and then it redirects to your home page. To go back to the home page without making changes, press the Cancel button.

  • Edit/update the record by changing the Car Name entry from “wigo” to “cool car.” Save the update by pressing the Save button. After you do that, it will redirect to your web application’s home page.

  • Your home page should now look something like this:

image shows the updated records in the homepage

Notice the last row of the database table record with “2” as the ID. Under the Review column, “cool car” is displayed in the same row for that particular record. The edit/update was a success!

Conclusion

This completes Part 8 of the multiple-series tutorial about creating a Java PostgreSQL web application. It’s the easy way to create a web application from scratch with Java using ECLIPSE IDE, Spring Data JPA, Spring MVC, Tomcat and PostgreSQL as the database. Today, you learned how to add the edit/update functionality so that you can modify record data and save those changes. Simplify your coding any time by reviewing this lesson, Part 8, and the previous ones in this series, Parts 1 through 7, titled “Java and PostgreSQL Web Application.”

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.