package cn.edu.wtbu.ec.im.isad;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class BookDAO {
private static final String INSERT_BOOK_SQL = "INSERT INTO books (title, author, available) VALUES (?, ?, ?)";
private static final String SELECT_BOOK_BY_ID = "SELECT * FROM books WHERE id = ?";
private static final String SELECT_ALL_BOOKS = "SELECT * FROM books LIMIT ?, ?";
private static final String UPDATE_BOOK_SQL = "UPDATE books SET title = ?, author = ?, available = ? WHERE id = ?";
private static final String DELETE_BOOK_SQL = "DELETE FROM books WHERE id = ?";
private static final String SEARCH_BOOKS = "SELECT * FROM books WHERE title LIKE ? OR author LIKE ? LIMIT ?, ?";
public Book createBook(Book book) {
try (Connection connection = DatabaseUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(INSERT_BOOK_SQL,
Statement.RETURN_GENERATED_KEYS)) {
preparedStatement.setString(1, book.getTitle());
preparedStatement.setString(2, book.getAuthor());
preparedStatement.setBoolean(3, book.isAvailable());
preparedStatement.executeUpdate();
ResultSet resultSet = preparedStatement.getGeneratedKeys();
if (resultSet.next()) {
int id = resultSet.getInt(1);
book.setId(id);
}
} catch (SQLException e) {
e.printStackTrace();
}
return book;
}
public Book getBookById(int id) {
Book book = null;
try (Connection connection = DatabaseUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(SELECT_BOOK_BY_ID)) {
preparedStatement.setInt(1, id);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
String title = resultSet.getString("title");
String author = resultSet.getString("author");
boolean available = resultSet.getBoolean("available");
book = new Book(id, title, author, available);
}
} catch (SQLException e) {
e.printStackTrace();
}
return book;
}
public List<Book> getAllBooks(int offset, int limit) {
List<Book> books = new ArrayList<>();
try (Connection connection = DatabaseUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ALL_BOOKS)) {
preparedStatement.setInt(1, offset);
preparedStatement.setInt(2, limit);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String title = resultSet.getString("title");
String author = resultSet.getString("author");
boolean available = resultSet.getBoolean("available");
books.add(new Book(id, title, author, available));
}
} catch (SQLException e) {
e.printStackTrace();
}
return books;
}
public boolean updateBook(Book book) {
boolean rowUpdated = false;
try (Connection connection = DatabaseUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_BOOK_SQL)) {
preparedStatement.setString(1, book.getTitle());
preparedStatement.setString(2, book.getAuthor());
preparedStatement.setBoolean(3, book.isAvailable());
preparedStatement.setInt(4, book.getId());
rowUpdated = preparedStatement.executeUpdate() > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return rowUpdated;
}
public boolean deleteBook(int id) {
boolean rowDeleted = false;
try (Connection connection = DatabaseUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(DELETE_BOOK_SQL)) {
preparedStatement.setInt(1, id);
rowDeleted = preparedStatement.executeUpdate() > 0;
} catch (SQLException e) {
e.printStackTrace();
}
return rowDeleted;
}
public List<Book> searchBooks(String keyword, int offset, int limit) {
List<Book> books = new ArrayList<>();
try (Connection connection = DatabaseUtil.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(SEARCH_BOOKS)) {
preparedStatement.setString(1, "%" + keyword + "%");
preparedStatement.setString(2, "%" + keyword + "%");
preparedStatement.setInt(3, offset);
preparedStatement.setInt(4, limit);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
int id = resultSet.getInt("id");
String title = resultSet.getString("title");
String author = resultSet.getString("author");
boolean available = resultSet.getBoolean("available");
books.add(new Book(id, title, author, available));
}
} catch (SQLException e) {
e.printStackTrace();
}
return books;
}
}
请完整补充上述代码的剩下内容