Servlet(viewItems)

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.sql.*;

@WebServlet(name = "viewItemsServlet", value = "/viewItems")
public class viewItems extends HttpServlet {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/nori";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "#1Hateno";

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Item> items = new ArrayList<>();

        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD)) {
            String sql = "SELECT * FROM items";
            try (PreparedStatement stmt = conn.prepareStatement(sql);
                 ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    int itemId = rs.getInt("item_id");
                    String title = rs.getString("title");
                    String author = rs.getString("author");
                    double price = rs.getDouble("price");
                    String description = rs.getString("description");
                    String category = rs.getString("category");
                    String availability = rs.getString("availability");

                    items.add(new Item(itemId, title, author, price, description, category, availability));
                    System.out.println("Fetched item: " + title);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        request.setAttribute("items", items);
        try {
            request.getRequestDispatcher("/pages/dashboard.jsp").forward(request, response);
        } catch (ServletException e) {
            throw new RuntimeException(e);
        }


    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

    }

    public void destroy() {
    }

    public static class Item {
        private int itemId;
        private String title;
        private String author;
        private double price;
        private String description;
        private String category;
        private String availability;

        public Item(int bookId, String title, String author, double price, String description, String category, String availability) {
            this.itemId = bookId;
            this.title = title;
            this.author = author;
            this.price = price;
            this.description = description;
            this.category = category;
            this.availability = availability;
        }

        // Getters for the book fields
        public int getItemId() {
            return itemId;
        }

        public String getTitle() {
            return title;
        }

        public String getAuthor() {
            return author;
        }

        public double getPrice() {
            return price;
        }

        public String getDescription() {
            return description;
        }

        public String getCategory() {
            return category;
        }

        public String getAvailability() {
            return availability;
        }

    }
}