0% found this document useful (0 votes)
2 views10 pages

Java Assignment

The document outlines the structure and implementation of an Online Book Management Portal using JSP for the frontend, Servlets for backend logic, and MySQL for the database. It includes details on the project file structure, client-tier JSP pages for adding, viewing, updating, and deleting books, as well as the logic tier implemented in the BookServlet class and the data access object (DAO) for database interactions. The project features CRUD operations, ensuring a modular and maintainable design.

Uploaded by

arya raj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Java Assignment

The document outlines the structure and implementation of an Online Book Management Portal using JSP for the frontend, Servlets for backend logic, and MySQL for the database. It includes details on the project file structure, client-tier JSP pages for adding, viewing, updating, and deleting books, as well as the logic tier implemented in the BookServlet class and the data access object (DAO) for database interactions. The project features CRUD operations, ensuring a modular and maintainable design.

Uploaded by

arya raj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Full Project: Online Book Management Portal (CRUD)

Tech Stack

• Frontend (Client Tier): JSP

• Backend Logic (Logic Tier): Servlet

• Database Layer (Data Tier): JDBC with MySQL

• Server: Apache Tomcat

• Database: MySQL

Project File Structure

BookStoreApp/

├── WebContent/

│ ├── [Link]

│ ├── [Link]

│ ├── [Link]

│ └── [Link]

└── src/com/bookstore/

├── [Link]

├── [Link]

└── [Link]

1. Client Tier (JSP Pages)

[Link] – Add Book

<%@ page contentType="text/html" %>

<html>
<head><title>Add Book</title></head>

<body>

<h2>Add New Book</h2>

<form action="BookServlet" method="post">

<input type="hidden" name="action" value="add">

ID: <input type="text" name="id"><br><br>

Title: <input type="text" name="title"><br><br>

Author: <input type="text" name="author"><br><br>

Price: <input type="text" name="price"><br><br>

<input type="submit" value="Add Book">

</form>

<a href="BookServlet?action=view">View All Books</a>

</body>

</html>

[Link] – Display All Books

<%@ page import="[Link].*, [Link].*" %>

<%

BookDAO dao = new BookDAO();

List<Book> list = [Link]();

%>

<html>

<head><title>Book List</title></head>

<body>

<h2>All Books</h2>

<table border="1">

<tr><th>ID</th><th>Title</th><th>Author</th><th>Price</th><th>Actions</th></tr>
<%

for (Book b : list) {

%>

<tr>

<td><%= [Link]() %></td>

<td><%= [Link]() %></td>

<td><%= [Link]() %></td>

<td><%= [Link]() %></td>

<td>

<a href="[Link]?id=<%=[Link]()%>">Edit</a> |

<a href="BookServlet?action=delete&id=<%= [Link]()%>">Delete</a>

</td>

</tr>

<%

%>

</table>

<a href="[Link]">Add New Book</a>

</body>

</html>

[Link] – Update Book

<%@ page import="[Link].*, [Link].*" %>

<%

int id = [Link]([Link]("id"));

BookDAO dao = new BookDAO();

Book book = [Link](id);


%>

<html>

<head><title>Update Book</title></head>

<body>

<h2>Update Book Information</h2>

<form action="BookServlet" method="post">

<input type="hidden" name="action" value="update">

<input type="hidden" name="id" value="<%= [Link]() %>">

Title: <input type="text" name="title" value="<%= [Link]() %>"><br><br>

Author: <input type="text" name="author" value="<%= [Link]() %>"><br><br>

Price: <input type="text" name="price" value="<%= [Link]() %>"><br><br>

<input type="submit" value="Update">

</form>

</body>

</html>

2. Logic Tier (Servlet)

[Link]

package [Link];

import [Link].*;

import [Link].*;

import [Link].*;

public class BookServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);
}

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String action = [Link]("action");

BookDAO dao = new BookDAO();

try {

if ("add".equals(action)) {

int id = [Link]([Link]("id"));

String title = [Link]("title");

String author = [Link]("author");

double price = [Link]([Link]("price"));

[Link](id, title, author, price);

[Link]("[Link]");

} else if ("update".equals(action)) {

int id = [Link]([Link]("id"));

String title = [Link]("title");

String author = [Link]("author");

double price = [Link]([Link]("price"));

[Link](id, title, author, price);

[Link]("[Link]");

} else if ("delete".equals(action)) {

int id = [Link]([Link]("id"));

[Link](id);
[Link]("[Link]");

} else if ("view".equals(action)) {

[Link]("[Link]");

} catch (Exception e) {

[Link]();

3. Data Tier (DAO + Model)

[Link]

package [Link];

public class Book {

private int id;

private String title;

private String author;

private double price;

public Book() {}

public Book(int id, String title, String author, double price) {

[Link] = id; [Link] = title; [Link] = author; [Link] = price;

public int getId() { return id; }


public String getTitle() { return title; }

public String getAuthor() { return author; }

public double getPrice() { return price; }

public void setId(int id) { [Link] = id; }

public void setTitle(String title) { [Link] = title; }

public void setAuthor(String author) { [Link] = author; }

public void setPrice(double price) { [Link] = price; }

[Link]

package [Link];

import [Link].*;

public class DBConnection {

public static Connection getConnection() {

Connection con = null;

try {

[Link]("[Link]");

con = [Link]("jdbc:mysql://localhost:3306/bookstore", "root",


"password");

} catch (Exception e) {

[Link]();

return con;

}
[Link]

package [Link];

import [Link].*;

import [Link].*;

public class BookDAO {

public boolean insertBook(int id, String title, String author, double price) {

try (Connection con = [Link]()) {

PreparedStatement ps = [Link]("INSERT INTO books VALUES (?, ?, ?,


?)");

[Link](1, id); [Link](2, title); [Link](3, author); [Link](4, price);

return [Link]() > 0;

} catch (Exception e) { [Link](); }

return false;

public List<Book> getAllBooks() {

List<Book> list = new ArrayList<>();

try (Connection con = [Link]()) {

ResultSet rs = [Link]().executeQuery("SELECT * FROM books");

while ([Link]())

[Link](new Book([Link](1), [Link](2), [Link](3), [Link](4)));

} catch (Exception e) { [Link](); }

return list;

}
public Book getBookById(int id) {

Book b = null;

try (Connection con = [Link]()) {

PreparedStatement ps = [Link]("SELECT * FROM books WHERE


id=?");

[Link](1, id);

ResultSet rs = [Link]();

if ([Link]())

b = new Book([Link](1), [Link](2), [Link](3), [Link](4));

} catch (Exception e) { [Link](); }

return b;

public boolean updateBook(int id, String title, String author, double price) {

try (Connection con = [Link]()) {

PreparedStatement ps = [Link]("UPDATE books SET title=?, author=?,


price=? WHERE id=?");

[Link](1, title);

[Link](2, author);

[Link](3, price);

[Link](4, id);

return [Link]() > 0;

} catch (Exception e) { [Link](); }

return false;

public boolean deleteBook(int id) {

try (Connection con = [Link]()) {


PreparedStatement ps = [Link]("DELETE FROM books WHERE id=?");

[Link](1, id);

return [Link]() > 0;

} catch (Exception e) { [Link](); }

return false;

4. Database Setup

CREATE DATABASE bookstore;

USE bookstore;

CREATE TABLE books (

id INT PRIMARY KEY,

title VARCHAR(100),

author VARCHAR(100),

price DOUBLE

);

5. Project Features

• Add Book: Insert records using [Link]

• View Books: Display all records via [Link]

• Update Book: Pre-filled edit form from [Link]

• Delete Book: Remove entry instantly

• Tier separation ensures modular, maintainable design

You might also like