DEV Community

Cover image for Java Backend Management Project
Dinesh Peram
Dinesh Peram

Posted on

Java Backend Management Project

Key Features:

  • CRUD operations on gadgets.
  • Clean and user-friendly interface.
  • Efficient handling of database operations using JDBC.
  • Modular structure following the MVC design pattern.

Technologies Used:

  • JDBC: For database connectivity and operations.
  • JSP (JavaServer Pages): For creating the user interface.
  • Java Beans: For the business logic and data model.
  • MVC Design Pattern: To organize the project into clear, manageable layers.

Understanding the MVC Architecture

The MVC (Model-View-Controller) pattern is central to the structure of this project, ensuring a clean separation of concerns. Each component plays a distinct role:

Model (Java Beans & JDBC):

  • The Model layer represents the application's business logic and interacts with the database.
  • Java Beans define the structure for the gadget data, including fields such as gadget ID, name, description, price, and availability.
  • JDBC handles the actual database interaction for executing SQL queries (INSERT, UPDATE, DELETE, SELECT).

Model Code:

package com.model;

public class Products {
    private int id;
    private String prodtype, location, mdate, prodname, prodprice;

    public Products() {} // Default constructor

    // Getter and Setter methods
    public int getId() { return id; }
    public void setId(int id) { this.id = id; }

    public String getProdtype() { return prodtype; }
    public void setProdtype(String prodtype) { this.prodtype = prodtype; }

    public String getLocation() { return location; }
    public void setLocation(String location) { this.location = location; }

    public String getMdate() { return mdate; }
    public void setMdate(String mdate) { this.mdate = mdate; }

    public String getProdname() { return prodname; }
    public void setProdname(String prodname) { this.prodname = prodname; }

    public String getProdprice() { return prodprice; }
    public void setProdprice(String prodprice) { this.prodprice = prodprice; }
}
Enter fullscreen mode Exit fullscreen mode

View (JSP Pages):

  • The View layer is responsible for displaying the data to the user. JSP pages are used to create a dynamic, interactive user interface.
  • It shows lists of gadgets, forms to add new gadgets, and options to update or delete them.
  • JSP pages use HTML and embedded Java code to present real-time data fetched from the Model.

JSP Template:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
    <title>Basic JSP Template</title>
</head>
<body>
    <h1>Welcome to JSP!</h1>

    <%
        String message = "Hello, this is a basic JSP page!";
    %>

    <p><%= message %></p>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Controller (Servlet):

  • The Controller acts as the intermediary between the View and the Model.
  • When a user interacts with the UI (for example, submits a form to add a gadget), the controller (Servlet) processes the request.
  • The Servlet retrieves the user inputs, calls the appropriate method in the Model (Java Bean), and then updates the database using JDBC.
  • Once the action is completed, the controller redirects the user to the appropriate JSP page to display the updated data or confirmation messages.

Add Controllers/ Servlets package for CRUD:

package com.example;
import java.io.IOException;
import statements

@WebServlet("/product")
public class ProductServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

// READ operation - Handle GET request
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        // Logic to fetch product by ID from the database
        response.getWriter().write("Product details for ID: " + id);
    }
}
Enter fullscreen mode Exit fullscreen mode

Flow of the Gadgets Management System

User Interaction (View):

  • The user interacts with the system through forms in JSP pages. For instance, a form allows the user to add a new gadget to the system.

Handling Requests (Controller):

  • When the form is submitted, the request is sent to the Servlet, which acts as the controller. The controller checks the action (whether it’s to add, update, or delete a gadget) and processes the request accordingly.

Database Operations (Model):

  • The Servlet calls appropriate methods in the Java Bean (Model) to interact with the database. For instance, it might call the addGadget() method, which uses JDBC to execute an SQL INSERT query in the database.

View Update:

  • After the database operation is performed, the Servlet forwards the request to a JSP page that shows the updated data (like a refreshed list of gadgets or a success message).

Flow Diagram


Key CRUD Operations in Action

Adding a Gadget:

  • The user submits a form with gadget details (name, description, price).
  • The controller retrieves this data and invokes the addGadget() method in the Java Bean.
  • JDBC executes an SQL query to insert the new gadget into the database.
  • The user is then redirected to a JSP page showing the updated gadget list.

Viewing Gadgets:

  • The user visits the gadgets page, where all existing gadgets are fetched from the database using the getAllGadgets() method.
  • JDBC runs a SELECT query to retrieve all the gadgets, and the data is displayed dynamically in the JSP page.

Updating a Gadget:

  • The user selects a gadget to update and fills out the updated details in a form.
  • The controller processes the form data and invokes updateGadget() in the Java Bean.
  • JDBC runs an UPDATE query to modify the gadget information in the database, and the user is redirected to the updated gadget list.

Deleting a Gadget:

  • The user clicks the "Delete" button next to a gadget in the list.
  • The controller processes the delete request and calls deleteGadget() in the Java Bean.
  • JDBC runs a DELETE query, removing the gadget from the database.

Conclusion

  • The Gadgets Management System project was a good opportunity to apply Java EE technologies in a real-world scenario. It strengthened my understanding of JDBC, JSP, and the MVC pattern, and gave me hands-on experience with Backend web development.

Check it out on GitHub:

GitHub

Top comments (0)