JavaBean+Servlet+JSP+DAO(2)设计实现一个图书管理系统。图书信息存放到一个数据库中,图书包含信息:图书号、图书名、作者、价格。请实现对图书的增删改查操作。
时间: 2024-03-21 12:43:02 浏览: 152
设计一个图书管理系统,实现增删改查,借书还书等操作
5星 · 资源好评率100%
好的,我会尽力回答您的问题。
首先,我们需要创建一个数据库来存储图书信息。可以使用MySQL或其他关系型数据库来实现。在数据库中,我们需要创建一个表来存储图书信息,表的字段包括图书号、图书名、作者、价格。
接下来,我们可以使用JavaBean来表示图书信息。我们可以创建一个Book类,它包含图书号、图书名、作者和价格这四个属性,并为每个属性提供getter和setter方法。例如:
```
public class Book {
private String bookId;
private String bookName;
private String author;
private double price;
public String getBookId() {
return bookId;
}
public void setBookId(String bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
```
接下来,我们可以使用DAO模式来实现对图书信息的增删改查操作。我们可以创建一个BookDAO接口,定义以下方法:
```
public interface BookDAO {
public List<Book> getAllBooks() throws SQLException;
public Book getBookById(String bookId) throws SQLException;
public void addBook(Book book) throws SQLException;
public void updateBook(Book book) throws SQLException;
public void deleteBook(String bookId) throws SQLException;
}
```
然后,我们可以创建一个BookDAOImpl类来实现BookDAO接口中的方法。例如:
```
public class BookDAOImpl implements BookDAO {
private Connection conn;
public BookDAOImpl(Connection conn) {
this.conn = conn;
}
public List<Book> getAllBooks() throws SQLException {
List<Book> books = new ArrayList<Book>();
String sql = "select * from books";
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
Book book = new Book();
book.setBookId(rs.getString("book_id"));
book.setBookName(rs.getString("book_name"));
book.setAuthor(rs.getString("author"));
book.setPrice(rs.getDouble("price"));
books.add(book);
}
return books;
}
public Book getBookById(String bookId) throws SQLException {
String sql = "select * from books where book_id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, bookId);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
Book book = new Book();
book.setBookId(rs.getString("book_id"));
book.setBookName(rs.getString("book_name"));
book.setAuthor(rs.getString("author"));
book.setPrice(rs.getDouble("price"));
return book;
} else {
return null;
}
}
public void addBook(Book book) throws SQLException {
String sql = "insert into books(book_id, book_name, author, price) values (?, ?, ?, ?)";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, book.getBookId());
ps.setString(2, book.getBookName());
ps.setString(3, book.getAuthor());
ps.setDouble(4, book.getPrice());
ps.executeUpdate();
}
public void updateBook(Book book) throws SQLException {
String sql = "update books set book_name=?, author=?, price=? where book_id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, book.getBookName());
ps.setString(2, book.getAuthor());
ps.setDouble(3, book.getPrice());
ps.setString(4, book.getBookId());
ps.executeUpdate();
}
public void deleteBook(String bookId) throws SQLException {
String sql = "delete from books where book_id=?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setString(1, bookId);
ps.executeUpdate();
}
}
```
最后,我们可以使用Servlet和JSP来实现用户界面。例如,我们可以创建一个BookServlet类,它处理所有的HTTP请求,并调用DAO中的方法来处理业务逻辑。例如:
```
public class BookServlet extends HttpServlet {
private BookDAO bookDAO;
public void init() throws ServletException {
Connection conn = getConnection(); // 获取数据库连接
bookDAO = new BookDAOImpl(conn); // 创建DAO对象
}
private Connection getConnection() throws ServletException {
Connection conn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/bookdb";
String username = "root";
String password = "root";
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
throw new ServletException(e);
} catch (SQLException e) {
throw new ServletException(e);
}
return conn;
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if (action == null) {
action = "list";
}
switch (action) {
case "list":
listBooks(request, response);
break;
case "show":
showBook(request, response);
break;
case "add":
addBook(request, response);
break;
case "edit":
editBook(request, response);
break;
case "update":
updateBook(request, response);
break;
case "delete":
deleteBook(request, response);
break;
default:
listBooks(request, response);
}
}
private void listBooks(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
List<Book> books = bookDAO.getAllBooks();
request.setAttribute("books", books);
RequestDispatcher dispatcher = request.getRequestDispatcher("/book_list.jsp");
dispatcher.forward(request, response);
} catch (SQLException e) {
throw new ServletException(e);
}
}
private void showBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String bookId = request.getParameter("bookId");
try {
Book book = bookDAO.getBookById(bookId);
request.setAttribute("book", book);
RequestDispatcher dispatcher = request.getRequestDispatcher("/book_show.jsp");
dispatcher.forward(request, response);
} catch (SQLException e) {
throw new ServletException(e);
}
}
private void addBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String bookId = request.getParameter("bookId");
String bookName = request.getParameter("bookName");
String author = request.getParameter("author");
double price = Double.parseDouble(request.getParameter("price"));
Book book = new Book();
book.setBookId(bookId);
book.setBookName(bookName);
book.setAuthor(author);
book.setPrice(price);
try {
bookDAO.addBook(book);
response.sendRedirect("BookServlet");
} catch (SQLException e) {
throw new ServletException(e);
}
}
private void editBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String bookId = request.getParameter("bookId");
try {
Book book = bookDAO.getBookById(bookId);
request.setAttribute("book", book);
RequestDispatcher dispatcher = request.getRequestDispatcher("/book_edit.jsp");
dispatcher.forward(request, response);
} catch (SQLException e) {
throw new ServletException(e);
}
}
private void updateBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String bookId = request.getParameter("bookId");
String bookName = request.getParameter("bookName");
String author = request.getParameter("author");
double price = Double.parseDouble(request.getParameter("price"));
Book book = new Book();
book.setBookId(bookId);
book.setBookName(bookName);
book.setAuthor(author);
book.setPrice(price);
try {
bookDAO.updateBook(book);
response.sendRedirect("BookServlet");
} catch (SQLException e) {
throw new ServletException(e);
}
}
private void deleteBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String bookId = request.getParameter("bookId");
try {
bookDAO.deleteBook(bookId);
response.sendRedirect("BookServlet");
} catch (SQLException e) {
throw new ServletException(e);
}
}
public void destroy() {
try {
conn.close(); // 关闭数据库连接
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
以上就是一个基本的图书管理系统的设计和实现。当用户发送HTTP请求时,BookServlet会调用相应的DAO方法来处理请求,并将处理结果通过JSP页面呈现给用户。
阅读全文