2、 模仿SystemUserDAO类,实现BookDAO类,并改造BookManager类,使之通过BookDAO操作数据库。 【实验结果与分析】 A、 给出BookDAO类代码。 B、 给出改造后BookManager类的各个方法的代码。
时间: 2024-03-09 14:44:17 浏览: 50
好的,针对你的问题,我可以提供以下解决方案:
A、BookDAO类代码
```
public class BookDAO {
private Connection conn;
public BookDAO(Connection conn) {
this.conn = conn;
}
public List<Book> getAllBooks() throws SQLException {
List<Book> books = new ArrayList<>();
String sql = "SELECT * FROM book";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
try (ResultSet rs = pstmt.executeQuery()) {
while (rs.next()) {
Book book = new Book(rs.getString("barcode"), rs.getString("book_name"), rs.getString("author"), rs.getString("publisher"));
books.add(book);
}
}
}
return books;
}
public Book getBookByBarcode(String barcode) throws SQLException {
String sql = "SELECT * FROM book WHERE barcode = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, barcode);
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
Book book = new Book(rs.getString("barcode"), rs.getString("book_name"), rs.getString("author"), rs.getString("publisher"));
return book;
} else {
return null;
}
}
}
}
public boolean addBook(Book book) throws SQLException {
String sql = "INSERT INTO book(barcode, book_name, author, publisher) VALUES(?, ?, ?, ?)";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, book.getBarcode());
pstmt.setString(2, book.getBookName());
pstmt.setString(3, book.getAuthor());
pstmt.setString(4, book.getPublisher());
int count = pstmt.executeUpdate();
return count > 0;
}
}
public boolean deleteBook(String barcode) throws SQLException {
String sql = "DELETE FROM book WHERE barcode = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, barcode);
int count = pstmt.executeUpdate();
return count > 0;
}
}
public boolean updateBook(Book book) throws SQLException {
String sql = "UPDATE book SET book_name = ?, author = ?, publisher = ? WHERE barcode = ?";
try (PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, book.getBookName());
pstmt.setString(2, book.getAuthor());
pstmt.setString(3, book.getPublisher());
pstmt.setString(4, book.getBarcode());
int count = pstmt.executeUpdate();
return count > 0;
}
}
}
```
B、改造后BookManager类的各个方法的代码
```
public class BookManager {
private BookDAO bookDAO;
public BookManager(BookDAO bookDAO) {
this.bookDAO = bookDAO;
}
public List<Book> getAllBooks() {
try {
return bookDAO.getAllBooks();
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public Book getBookByBarcode(String barcode) {
try {
return bookDAO.getBookByBarcode(barcode);
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public boolean addBook(Book book) {
try {
return bookDAO.addBook(book);
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public boolean deleteBook(String barcode) {
try {
return bookDAO.deleteBook(barcode);
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
public boolean updateBook(Book book) {
try {
return bookDAO.updateBook(book);
} catch (SQLException e) {
e.printStackTrace();
return false;
}
}
}
```
以上是模仿SystemUserDAO类,实现BookDAO类,并改造BookManager类,使之通过BookDAO操作数据库的解决方案。其中,BookDAO类用于对图书信息的增删改查操作,BookManager类用于调用BookDAO类中的方法以实现对图书信息的管理。需要注意的是,以上代码仅供参考,具体实现需要根据实际情况进行调整。
阅读全文