forked from renz45/Java-BookStore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookDAO.java
More file actions
123 lines (98 loc) · 3.35 KB
/
Copy pathBookDAO.java
File metadata and controls
123 lines (98 loc) · 3.35 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
package com.pluralsight;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class BookDAO {
private Connection jdbcConnection;
public BookDAO(Connection connection)
{
jdbcConnection = connection;
}
public Book getBook(int id) {
Book book = null;
String sql = "SELECT * FROM book WHERE id = ?";
try {
PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setInt(1, id);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
String title = resultSet.getString("title");
String author = resultSet.getString("author");
float price = resultSet.getFloat("price");
book = new Book(id, title, author, price);
}
resultSet.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return book;
}
public ArrayList<Book> listAllBooks() {
ArrayList<Book> listBook = new ArrayList<>();
String sql = "SELECT * FROM book";
try {
Statement statement = jdbcConnection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
int id = resultSet.getInt("id");
String title = resultSet.getString("title");
String author = resultSet.getString("author");
float price = resultSet.getFloat("price");
Book book = new Book(id, title, author, price);
listBook.add(book);
}
resultSet.close();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
return listBook;
}
public boolean insertBook(Book book) {
String sql = "INSERT INTO book (title, author, price) VALUES (?, ?, ?)";
try {
PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setString(1, book.getTitle());
statement.setString(2, book.getAuthor());
statement.setFloat(3, book.getPrice());
boolean rowInserted = statement.executeUpdate() > 0;
statement.close();
return rowInserted;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public void deleteBook(int id) {
String SQL = "DELETE FROM book WHERE id = ?";
try {
PreparedStatement statement = jdbcConnection.prepareStatement(SQL);
statement.setInt(1, id);
statement.executeUpdate();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateBook(Book book) {
String SQL = "UPDATE book SET title = ?, author = ?, price = ? " +
"WHERE id = ?";
try {
PreparedStatement statement = jdbcConnection.prepareStatement(SQL);
statement.setString(1, book.getTitle());
statement.setString(2, book.getAuthor());
statement.setFloat(3, book.getPrice());
statement.setInt(4, book.getId());
statement.executeUpdate();
statement.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}