-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchBookAction.jsp
More file actions
120 lines (111 loc) · 4.65 KB
/
searchBookAction.jsp
File metadata and controls
120 lines (111 loc) · 4.65 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
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ 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>Search Results</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: 70vh;
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: 450px;
margin: auto;
margin-top: 20px;
}
</style>
</head>
<body style="background-image: url('user-background.jpg'); background-size: cover; background-position: center;">
<div class="toolbar">
<a href="userDashboard.jsp">Student Dashboard</a>
<a href="viewBooks.jsp">View Books</a>
<a href="searchBook.jsp">Search Book</a>
<a href="viewProfile.jsp">View Profile</a>
</div>
<div class="form-container">
<h1>Search Results</h1>
<%
// Fetch search criteria
String title = request.getParameter("title");
String author = request.getParameter("author");
String category = request.getParameter("category");
// Establish DB connection
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
// Initialize DB connection
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/LibraryDB", "root", "Mahadev07ji$");
// Prepare the SQL query with the dynamic conditions
String sql = "SELECT * FROM books WHERE 1=1";
if (title != null && !title.isEmpty()) {
sql += " AND title LIKE ?";
}
if (author != null && !author.isEmpty()) {
sql += " AND author LIKE ?";
}
if (category != null && !category.isEmpty()) {
sql += " AND category LIKE ?";
}
stmt = conn.prepareStatement(sql);
// Set parameters dynamically based on the search criteria
int paramIndex = 1;
if (title != null && !title.isEmpty()) {
stmt.setString(paramIndex++, "%" + title + "%");
}
if (author != null && !author.isEmpty()) {
stmt.setString(paramIndex++, "%" + author + "%");
}
if (category != null && !category.isEmpty()) {
stmt.setString(paramIndex++, "%" + category + "%");
}
// Execute query and fetch results
rs = stmt.executeQuery();
if (rs.next()) {
out.println("<table class='results-table'>");
out.println("<tr><th>Title</th><th>Author</th><th>Category</th><th>Quantity</th><th>Description</th></tr>");
do {
out.println("<tr>");
out.println("<td>" + rs.getString("title") + "</td>");
out.println("<td>" + rs.getString("author") + "</td>");
out.println("<td>" + rs.getString("category") + "</td>");
out.println("<td>" + rs.getInt("quantity") + "</td>");
out.println("<td>" + rs.getString("description") + "</td>");
out.println("</tr>");
} while (rs.next());
out.println("</table>");
} else {
out.println("<p>No books found matching the criteria.</p>");
}
} catch (Exception e) {
out.println("<p>Error: " + e.getMessage() + "</p>");
} finally {
try {
if (rs != null) rs.close();
if (stmt != null) stmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
out.println("<p>Error closing resources: " + e.getMessage() + "</p>");
}
}
%>
</div>
<script src="script.js"></script>
</body>
</html>