1.
0 Rationale
Servlets in Java are essential for developing dynamic and interactive web
applications. They enable server-side processing, allowing for the generation of
dynamic content, handling user requests, and interacting with databases. Servlets offer
a robust and scalable approach to web development, as they can manage complex
business logic and maintain state between multiple client interactions. They also
provide a foundation for building RESTFUL APIs and integrating with various
frameworks and libraries. By performing a servlet program in Java, developers can
create efficient and feature-rich web applications that offer a seamless user experience
and meet the demands of modern web development.
2.0 Aim/Benefits of the Micro-Project
To Perform CURD operations using Servlet.
Benefits:
a) Able to perform operations such as create, update, read and delete
b) Able to execute curd operations in servlet.
3.0 Course Outcomes Addressed
1. Develop program using servlet.
Actual Methodology Followed
1. Gathered Information:
Gathered all the possible information about CURD operation from various sources
like:
a) Books on Advance Java.
b) Reference links / websites - Geeksforgeeks and Tutorials Point etc.
c) Online tutorials on the given title.
2. Literature Review:
All the information collected from various sources, such as books, articles,
reports, manuals, websites, and more, regarding the CURD operations and
implementation of these operations has been thoroughly reviewed and discussed
within our project group.
JDBC
JDBC stands for Java Database Connectivity. It is a Java-based API (Application
Programming Interface) that allows Java applications to interact with relational
databases. JDBC provides a standardized way for Java programs to connect to and
manipulate data stored in databases, such as MySQL, Oracle, PostgreSQL, Microsoft
SQL Server, and many others.
Here are some key aspects of JDBC:
Database Connectivity: JDBC allows Java applications to establish connections to
databases. It provides the necessary classes and methods to connect to a database
server.
Database Operations: JDBC enables developers to perform various database
operations, such as executing SQL queries, retrieving data from databases,
inserting, updating, and deleting records.
Database Drivers: JDBC uses database-specific drivers to connect to different
database systems. These drivers are provided by the database vendors or can be
third-party drivers that implement the JDBC API.
API Components: JDBC consists of a set of Java classes and interfaces found in
the [Link] and [Link] packages. Common classes/interfaces include
Connection, Statement, ResultSet, and DataSource.
Connection Pooling: Many JDBC frameworks and libraries also support
connection pooling, which helps manage and reuse database connections
efficiently.
Java Database Connectivity with 5 Steps
There are 5 steps to connect any java application with the
database using JDBC. These steps are as follows:
1. Register the Driver class
2. Create connection
3. Create statement
4. Execute queries
5. Close connection
4.0 Actual Resources Used
Sr. Name of
Specification
No. Resources/Material
Computer system HP i3 processor, 11th generation,4gb RAM,
1.
256 GB SDD
2. Operating system Windows 10 Pro
3. Software [Link]-Word
5.0 Code
1. Database Connection Class
This class handles connecting to the database.
public class DatabaseConnection {
private static final String URL = "jdbc:mysql://localhost:3306/bookstore";
private static final String USERNAME = "root";
private static final String PASSWORD = "password";
public static Connection getConnection() throws SQLException {
return [Link](URL, USERNAME, PASSWORD);
}
}
2. BookServlet for CRUD Operations
The BookServlet handles different HTTP methods for CRUD operations.
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link].*;
@WebServlet("/books")
public class BookServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// CREATE operation
String title = [Link]("title");
String author = [Link]("author");
double price = [Link]([Link]("price"));
int quantity = [Link]([Link]("quantity"));
try (Connection conn = [Link]();
PreparedStatement stmt = [Link]("INSERT INTO books (title,
author, price, quantity) VALUES (?, ?, ?, ?)")) {
[Link](1, title);
[Link](2, author);
[Link](3, price);
[Link](4, quantity);
[Link]();
[Link]().println("Book added successfully.");
} catch (SQLException e) {
[Link]();
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// READ operation
try (Connection conn = [Link]();
Statement stmt = [Link]();
ResultSet rs = [Link]("SELECT * FROM books")) {
while ([Link]()) {
[Link]().println("ID: " + [Link]("id") + ", Title: " +
[Link]("title") + ", Author: " + [Link]("author") + ", Price: " +
[Link]("price") + ", Quantity: " + [Link]("quantity"));
} catch (SQLException e) {
[Link]();
}
protected void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// UPDATE operation
int id = [Link]([Link]("id"));
String title = [Link]("title");
String author = [Link]("author");
double price = [Link]([Link]("price"));
int quantity = [Link]([Link]("quantity"));
try (Connection conn = [Link]();
PreparedStatement stmt = [Link]("UPDATE books SET title=?,
author=?, price=?, quantity=? WHERE id=?")) {
[Link](1, title);
[Link](2, author);
[Link](3, price);
[Link](4, quantity);
[Link](5, id);
[Link]();
[Link]().println("Book updated successfully.");
} catch (SQLException e) {
[Link]();
}
protected void doDelete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// DELETE operation
int id = [Link]([Link]("id"));
try (Connection conn = [Link]();
PreparedStatement stmt = [Link]("DELETE FROM books
WHERE id=?")) {
[Link](1, id);
[Link]();
[Link]().println("Book deleted successfully.");
} catch (SQLException e) {
[Link]();
}
6.0 Output