-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathissueBookAction.jsp
More file actions
132 lines (122 loc) · 4.36 KB
/
issueBookAction.jsp
File metadata and controls
132 lines (122 loc) · 4.36 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
<%@ 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>Issue 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;
}
</style>
</head>
<body>
<%
// Declare connection variable outside try block
Connection conn = null;
PreparedStatement stmt = null;
try {
// Retrieve form data
String userIdStr = request.getParameter("userId");
String bookIdStr = request.getParameter("bookId");
String issueDateStr = request.getParameter("issueDate");
String returnDateStr = request.getParameter("returnDate");
// Check if any required parameter is null
if (userIdStr == null || bookIdStr == null || issueDateStr == null || returnDateStr == null) {
%>
<div>
<h2>Error: All fields must be filled.</h2>
<a href="issueBook.jsp">Go back</a>
</div>
<%
return; // Stop further execution if parameters are missing
}
// Parse the parameters
String userId = userIdStr;
int bookId = Integer.parseInt(bookIdStr);
// Convert issue and return date to java.sql.Date
java.sql.Date issueDate = java.sql.Date.valueOf(issueDateStr);
java.sql.Date returnDate = java.sql.Date.valueOf(returnDateStr);
// 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 book is available
String checkSql = "SELECT quantity FROM books WHERE id = ?";
stmt = conn.prepareStatement(checkSql);
stmt.setInt(1, bookId);
java.sql.ResultSet rs = stmt.executeQuery();
if (rs.next() && rs.getInt("quantity") > 0) {
// Update book quantity
String updateBookSql = "UPDATE books SET quantity = quantity - 1 WHERE id = ?";
stmt = conn.prepareStatement(updateBookSql);
stmt.setInt(1, bookId);
stmt.executeUpdate();
// Insert borrow record
String insertSql = "INSERT INTO borrowed_books (user_id, book_id, borrow_date, return_date) VALUES (?, ?, ?, ?)";
stmt = conn.prepareStatement(insertSql);
stmt.setString(1, userId);
stmt.setInt(2, bookId);
stmt.setDate(3, issueDate);
stmt.setDate(4, returnDate);
int rowsInserted = stmt.executeUpdate();
if (rowsInserted > 0) {
%>
<div class="success-message">
<h2>Book Issued Successfully!</h2>
<p>Book ID: <%= bookId %></p>
<p>User ID: <%= userId %></p>
<p>Issue Date: <%= issueDate %></p>
<p>Return Date: <%= returnDate %></p>
<a href="adminDashboard.jsp" class="back-button">Go back to Dashboard</a>
</div>
<%
} else {
%>
<div class="error-message">
<h2>Error: Could not issue book.</h2>
<a href="issueBook.jsp">Try Again</a>
</div>
<%
}
} else {
%>
<div class="error-message">
<h2>Error: Book is not available for issuing.</h2>
<a href="issueBook.jsp">Try Again</a>
</div>
<%
}
} catch (Exception e) {
%>
<div>
<h2>Error: <%= e.getMessage() %></h2>
<a href="issueBook.jsp">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>