-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookDAO.java
More file actions
71 lines (61 loc) · 2.11 KB
/
BookDAO.java
File metadata and controls
71 lines (61 loc) · 2.11 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
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class BookDAO {
private Connection conn;
public BookDAO() {
conn = DatabaseUtil.connect();
createTable();
}
private void createTable() {
String sql = "CREATE TABLE IF NOT EXISTS books (id INTEGER PRIMARY KEY, name TEXT, ISBN TEXT)";
try (Statement stmt = conn.createStatement()) {
stmt.execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void addBook(Book book) {
String sql = "INSERT INTO books(name, ISBN) VALUES(?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, book.getBookName());
pstmt.setString(2, book.getISBN());
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<Book> getAllBooks() {
List<Book> books = new ArrayList<>();
String sql = "SELECT * FROM books";
try (Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
books.add(new Book(rs.getInt("id"), rs.getString("name"), rs.getString("ISBN")));
}
} catch (SQLException e) {
e.printStackTrace();
}
return books;
}
public void updateBook(int id, String name, String isbn) {
String sql = "UPDATE books SET name = ?, ISBN = ? WHERE id = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, name);
pstmt.setString(2, isbn);
pstmt.setInt(3, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteBook(int id) {
String sql = "DELETE FROM books WHERE id = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, id);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}