-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookDAO.java
More file actions
180 lines (149 loc) · 6.85 KB
/
BookDAO.java
File metadata and controls
180 lines (149 loc) · 6.85 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package com.dao;
import com.db.DBConnect;
import com.user.Book;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class BookDAO {
private Connection conn;
public BookDAO(Connection conn) {
this.conn = conn;
}
public List<Book> getAllBooks() {
List<Book> books = new ArrayList<>();
try {
String query = "SELECT * FROM books ORDER BY created_at DESC";
PreparedStatement ps = conn.prepareStatement(query);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Book book = new Book();
book.setId(rs.getInt("id"));
book.setTitle(rs.getString("title"));
book.setAuthor(rs.getString("author"));
book.setPrice(rs.getDouble("price"));
book.setCategory(rs.getString("category"));
book.setDescription(rs.getString("description"));
book.setImagePath(rs.getString("image_path"));
books.add(book);
}
System.out.println("Retrieved " + books.size() + " books from database");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Error in getAllBooks: " + e.getMessage());
}
return books;
}
public List<Book> getBooksByCategory(String category) {
List<Book> books = new ArrayList<>();
try {
String query = "SELECT * FROM books WHERE category = ? ORDER BY created_at DESC";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, category);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Book book = new Book();
book.setId(rs.getInt("id"));
book.setTitle(rs.getString("title"));
book.setAuthor(rs.getString("author"));
book.setPrice(rs.getDouble("price"));
book.setCategory(rs.getString("category"));
book.setDescription(rs.getString("description"));
book.setImagePath(rs.getString("image_path"));
books.add(book);
}
System.out.println("Retrieved " + books.size() + " books for category: " + category);
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Error in getBooksByCategory: " + e.getMessage());
}
return books;
}
public Book getBookById(int id) {
Book book = null;
try {
String query = "SELECT * FROM books WHERE id = ?";
PreparedStatement ps = conn.prepareStatement(query);
ps.setInt(1, id);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
book = new Book();
book.setId(rs.getInt("id"));
book.setTitle(rs.getString("title"));
book.setAuthor(rs.getString("author"));
book.setPrice(rs.getDouble("price"));
book.setCategory(rs.getString("category"));
book.setDescription(rs.getString("description"));
book.setImagePath(rs.getString("image_path"));
}
System.out.println("Retrieved book by ID " + id + ": " + (book != null ? book.getTitle() : "NOT FOUND"));
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Error in getBookById: " + e.getMessage());
}
return book;
}
public boolean addBook(Book book) {
boolean success = false;
try {
String query = "INSERT INTO books(title, author, price, category, description, image_path) VALUES(?, ?, ?, ?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(query);
ps.setString(1, book.getTitle());
ps.setString(2, book.getAuthor());
ps.setDouble(3, book.getPrice());
ps.setString(4, book.getCategory());
ps.setString(5, book.getDescription());
ps.setString(6, book.getImagePath());
int rowsAffected = ps.executeUpdate();
success = rowsAffected > 0;
System.out.println("Book addition attempted: " + (success ? "SUCCESS" : "FAILED"));
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Error in addBook: " + e.getMessage());
}
return success;
}
public List<String> getAllCategories() {
List<String> categories = new ArrayList<>();
try {
String query = "SELECT DISTINCT category FROM books ORDER BY category";
PreparedStatement ps = conn.prepareStatement(query);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
categories.add(rs.getString("category"));
}
System.out.println("Retrieved " + categories.size() + " categories from database");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Error in getAllCategories: " + e.getMessage());
}
return categories;
}
public List<Book> searchBooks(String searchTerm) {
List<Book> books = new ArrayList<>();
try {
String query = "SELECT * FROM books WHERE title LIKE ? OR author LIKE ? OR category LIKE ? ORDER BY title";
PreparedStatement ps = conn.prepareStatement(query);
String likeTerm = "%" + searchTerm + "%";
ps.setString(1, likeTerm);
ps.setString(2, likeTerm);
ps.setString(3, likeTerm);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Book book = new Book();
book.setId(rs.getInt("id"));
book.setTitle(rs.getString("title"));
book.setAuthor(rs.getString("author"));
book.setPrice(rs.getDouble("price"));
book.setCategory(rs.getString("category"));
book.setDescription(rs.getString("description"));
book.setImagePath(rs.getString("image_path"));
books.add(book);
}
System.out.println("Search for '" + searchTerm + "' returned " + books.size() + " books");
} catch (SQLException e) {
e.printStackTrace();
System.out.println("Error in searchBooks: " + e.getMessage());
}
return books;
}
}