-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreturnBookAction.jsp
More file actions
138 lines (125 loc) · 4.49 KB
/
returnBookAction.jsp
File metadata and controls
138 lines (125 loc) · 4.49 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<%@ page import="java.sql.Connection, java.sql.DriverManager, java.sql.PreparedStatement, java.sql.SQLException" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Return Book</title>
<link rel="stylesheet" href="dashboard.css">
<style>
.success-message, .error-message {
background-color: rgba(45, 44, 44, 0.8);
padding: 20px;
margin-top: 20px;
border-radius: 22px;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
text-align: center;
max-width: 400px;
margin-left: 400px;
}
.success-message h2, p {
color: #ffffff;
font-size: 24px;
margin-bottom: 20px;
align-items: center;
}
.error-message h2 {
color: #ff4d4d;
font-size: 24px;
margin-bottom: 20px;
align-items: center;
}
.back-button {
color: #fff;
background-color: #007bff;
padding: 10px 20px;
text-decoration: none;
border-radius: 5px;
font-size: 16px;
}
.back-button:hover {
background-color: #0056b3;
}
</style>
</head>
<body>
<%
// Declare connection variable outside try block
Connection conn = null;
PreparedStatement stmt = null;
try {
// Retrieve form data using correct parameter names
String userIdStr = request.getParameter("user_id");
String bookIdStr = request.getParameter("book_id");
// Debugging output to ensure parameters are being passed
out.println("userId: " + userIdStr);
out.println("bookId: " + bookIdStr);
// Check if any required parameter is null or empty
if (userIdStr == null || bookIdStr == null || userIdStr.isEmpty() || bookIdStr.isEmpty()) {
%>
<div class="error-message">
<h2>Error: All fields must be filled.</h2>
<a href="returnBook.jsp" class="back-button">Go back</a>
</div>
<%
return; // Stop further execution if parameters are missing
}
// Parse the parameters
int userId = Integer.parseInt(userIdStr);
int bookId = Integer.parseInt(bookIdStr);
// Establish connection to the database
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/LibraryDB", "root", "Mahadev07ji$");
// Check if the borrow record exists in borrowed_books table
String checkSql = "SELECT * FROM borrowed_books WHERE user_id = ? AND book_id = ? ";
stmt = conn.prepareStatement(checkSql);
stmt.setInt(1, userId);
stmt.setInt(2, bookId);
java.sql.ResultSet rs = stmt.executeQuery();
if (rs.next()) {
// Remove the record from borrowed_books table
String deleteSql = "DELETE FROM borrowed_books WHERE user_id = ? AND book_id = ?";
stmt = conn.prepareStatement(deleteSql);
stmt.setInt(1, userId);
stmt.setInt(2, bookId);
stmt.executeUpdate();
// Update the book quantity in books table
String updateBookSql = "UPDATE books SET quantity = quantity + 1 WHERE id = ?";
stmt = conn.prepareStatement(updateBookSql);
stmt.setInt(1, bookId);
stmt.executeUpdate();
%>
<div class="success-message">
<h2>Book Returned Successfully!</h2>
<p>Book ID: <%= bookId %></p>
<p>User ID: <%= userId %></p>
<a href="adminDashboard.jsp" class="back-button">Go back to Dashboard</a>
</div>
<%
} else {
%>
<div class="error-message">
<h2>Error: No record found for the borrowed book.</h2>
<a href="returnBook.jsp" class="back-button">Try Again</a>
</div>
<%
}
} catch (Exception e) {
%>
<div class="error-message">
<h2>Error: <%= e.getMessage() %></h2>
<a href="returnBook.jsp" class="back-button">Try Again</a>
</div>
<%
} finally {
// Close resources
try {
if (conn != null) conn.close();
if (stmt != null) stmt.close();
} catch (SQLException e) {
out.println("<h2>Error closing resources: " + e.getMessage() + "</h2>");
}
}
%>
</body>
</html>