0% found this document useful (0 votes)
22 views

Aditya JAVA PROJECT

Uploaded by

shahajis3486
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Aditya JAVA PROJECT

Uploaded by

shahajis3486
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

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 java.sql and javax.sql 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 1.MS-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 DriverManager.getConnection(URL, USERNAME, PASSWORD);
}
}
2. BookServlet for CRUD Operations

The BookServlet handles different HTTP methods for CRUD operations.

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.*;

import java.io.IOException;

import java.sql.*;

@WebServlet("/books")

public class BookServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {

// CREATE operation

String title = request.getParameter("title");

String author = request.getParameter("author");

double price = Double.parseDouble(request.getParameter("price"));

int quantity = Integer.parseInt(request.getParameter("quantity"));

try (Connection conn = DatabaseConnection.getConnection();

PreparedStatement stmt = conn.prepareStatement("INSERT INTO books (title,


author, price, quantity) VALUES (?, ?, ?, ?)")) {

stmt.setString(1, title);

stmt.setString(2, author);

stmt.setDouble(3, price);
stmt.setInt(4, quantity);

stmt.executeUpdate();

response.getWriter().println("Book added successfully.");

} catch (SQLException e) {

e.printStackTrace();

protected void doGet(HttpServletRequest request, HttpServletResponse response)


throws ServletException, IOException {

// READ operation

try (Connection conn = DatabaseConnection.getConnection();

Statement stmt = conn.createStatement();

ResultSet rs = stmt.executeQuery("SELECT * FROM books")) {

while (rs.next()) {

response.getWriter().println("ID: " + rs.getInt("id") + ", Title: " +


rs.getString("title") + ", Author: " + rs.getString("author") + ", Price: " +
rs.getDouble("price") + ", Quantity: " + rs.getInt("quantity"));

} catch (SQLException e) {

e.printStackTrace();

}
protected void doPut(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// UPDATE operation

int id = Integer.parseInt(request.getParameter("id"));

String title = request.getParameter("title");

String author = request.getParameter("author");

double price = Double.parseDouble(request.getParameter("price"));

int quantity = Integer.parseInt(request.getParameter("quantity"));

try (Connection conn = DatabaseConnection.getConnection();

PreparedStatement stmt = conn.prepareStatement("UPDATE books SET title=?,


author=?, price=?, quantity=? WHERE id=?")) {

stmt.setString(1, title);

stmt.setString(2, author);

stmt.setDouble(3, price);

stmt.setInt(4, quantity);

stmt.setInt(5, id);

stmt.executeUpdate();

response.getWriter().println("Book updated successfully.");

} catch (SQLException e) {

e.printStackTrace();

}
protected void doDelete(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// DELETE operation

int id = Integer.parseInt(request.getParameter("id"));

try (Connection conn = DatabaseConnection.getConnection();

PreparedStatement stmt = conn.prepareStatement("DELETE FROM books


WHERE id=?")) {

stmt.setInt(1, id);

stmt.executeUpdate();

response.getWriter().println("Book deleted successfully.");

} catch (SQLException e) {

e.printStackTrace();

}
6.0 Output

You might also like