-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeleteBook.jsp
More file actions
121 lines (113 loc) · 4.33 KB
/
deleteBook.jsp
File metadata and controls
121 lines (113 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.SQLException" %>
<%@ page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Delete Book</title>
<link rel="stylesheet" href="dashboard.css">
<link rel="stylesheet" href="userStyle.css">
<style>
.form-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 60vh;
background: rgba(0, 0, 0, 0.5); /* Dark background with transparency */
padding: 20px;
border-radius: 10px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.5);
width: 80%;
max-width: 380px;
margin: auto;
margin-top: 20px;
}
.toolbar .active {
background-color: #d9c8c8;
color: rgb(18, 18, 18);
border-radius: 10px;
}
</style>
</head>
<body style="background-image: url('admin-background.jpg'); background-size: cover; background-position: center;">
<div class="toolbar">
<a href="adminDashboard.jsp">Admin Dashboard</a>
<a href="addBook.jsp">Add Books</a>
<a href="selectBookId.jsp">Update Book</a>
<a href="deleteBook.jsp">Delete Book</a>
<a href="issueBook.jsp">Issue Book</a>
<a href="returnBook.jsp">Return Book</a>
</div>
<div class="form-container">
<h1>Delete Book</h1>
<%
String bookIdParam = request.getParameter("id");
if (bookIdParam == null || bookIdParam.isEmpty()) {
// Step 1: Ask the user to input the book ID to delete
%>
<form action="deleteBook.jsp" method="get" class="book-form">
<div class="form-group">
<label for="id">Enter Book ID to Delete:</label>
<input type="number" id="id" name="id" required placeholder="Enter Book ID">
</div>
<button type="submit" class="submit-btn">Delete Book</button>
</form>
<%
} else {
// Step 2: Process the book deletion after ID submission
int bookId = Integer.parseInt(bookIdParam);
Connection con = null;
PreparedStatement ps = null;
try {
// Load MySQL Driver
Class.forName("com.mysql.cj.jdbc.Driver");
// Connect to the database
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/LibraryDB", "root", "Mahadev07ji$");
// Create SQL query for deleting the book
String sql = "DELETE FROM Books WHERE id = ?";
// Prepare statement to prevent SQL Injection
ps = con.prepareStatement(sql);
ps.setInt(1, bookId);
// Execute the delete
int rowsAffected = ps.executeUpdate();
if (rowsAffected > 0) {
%>
<div class="success-message">
<h2>Book Deleted Successfully!</h2>
<a href="adminDashboard.jsp" class="back-button">Go back to Dashboard</a>
</div>
<%
} else {
%>
<div class="error-message">
<h2>Error: Book Deletion Failed. ID not found.</h2>
<a href="deleteBook.jsp" class="back-button">Try Again</a>
</div>
<%
}
} catch (Exception e) {
%>
<div class="error-message">
<h2>Error: <%= e.getMessage() %></h2>
<a href="deleteBook.jsp" class="back-button">Try Again</a>
</div>
<%
} finally {
try {
if (ps != null) ps.close();
if (con != null) con.close();
} catch (SQLException e) {
out.println("<h2>Error closing resources: " + e.getMessage() + "</h2>");
}
}
}
%>
</div>
<script src="script.js"></script>
</body>
</html>