-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookRepository.java
More file actions
105 lines (95 loc) · 3.55 KB
/
Copy pathBookRepository.java
File metadata and controls
105 lines (95 loc) · 3.55 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
import javax.swing.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class BookRepository {
private static final String[][] FAMOUS_BOOKS = {
{"The Alchemist", "Paulo Coelho"},
{"Harry Potter and the Sorcerer's Stone", "J.K. Rowling"},
{"The Hobbit", "J.R.R. Tolkien"},
{"To Kill a Mockingbird", "Harper Lee"},
{"Pride and Prejudice", "Jane Austen"},
{"The Great Gatsby", "F. Scott Fitzgerald"},
{"1984", "George Orwell"},
{"The Catcher in the Rye", "J.D. Salinger"},
{"The Kite Runner", "Khaled Hosseini"},
{"The Lord of the Rings", "J.R.R. Tolkien"}
};
private BookRepository() {
}
public static void initializeLibraryData() {
Connection con = DBConnection.getConnection();
if (con == null) {
return;
}
try (Statement createTable = con.createStatement()) {
// Create table first
createTable.executeUpdate(
"CREATE TABLE IF NOT EXISTS books ("
+ "id INT PRIMARY KEY AUTO_INCREMENT,"
+ "name VARCHAR(255) NOT NULL,"
+ "author VARCHAR(255) NOT NULL,"
+ "issued BOOLEAN NOT NULL DEFAULT FALSE)"
);
// Then check if we need to seed data
try (PreparedStatement countStatement = con.prepareStatement("SELECT COUNT(*) FROM books")) {
ResultSet countResult = countStatement.executeQuery();
if (countResult.next() && countResult.getInt(1) == 0) {
seedFamousBooks(con);
}
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Unable to initialize library data.\n" + ex.getMessage());
}
}
public static List<BookItem> getAllBooks() {
List<BookItem> books = new ArrayList<>();
Connection con = DBConnection.getConnection();
if (con == null) {
return books;
}
try (
PreparedStatement ps = con.prepareStatement("SELECT * FROM books ORDER BY name");
ResultSet rs = ps.executeQuery()
) {
while (rs.next()) {
books.add(new BookItem(
rs.getInt("id"),
rs.getString("name"),
rs.getString("author"),
rs.getBoolean("issued")
));
}
} catch (Exception ex) {
JOptionPane.showMessageDialog(null, "Unable to load books.\n" + ex.getMessage());
}
return books;
}
public static void loadBooksInto(JComboBox<BookItem> comboBox, boolean onlyAvailable, boolean onlyIssued) {
comboBox.removeAllItems();
for (BookItem book : getAllBooks()) {
if (onlyAvailable && book.isIssued()) {
continue;
}
if (onlyIssued && !book.isIssued()) {
continue;
}
comboBox.addItem(book);
}
}
public static int getBookCount() {
return getAllBooks().size();
}
private static void seedFamousBooks(Connection con) throws SQLException {
try (PreparedStatement insert = con.prepareStatement(
"INSERT INTO books(name, author, issued) VALUES(?,?,false)"
)) {
for (String[] book : FAMOUS_BOOKS) {
insert.setString(1, book[0]);
insert.setString(2, book[1]);
insert.addBatch();
}
insert.executeBatch();
}
}
}