设计实现一个图书管理系统。图书信息存放到一个数据库中,图书包含信息:图书号、图书名、作者、价格。请实现对图书的增删改查操作。采用JavaBean+Servlet+JSP+DAO的Web架构设计该系统
时间: 2024-03-22 16:36:53 浏览: 234
设计实现一个图书管理系统。图书信息存放到一个数据库中。图书
好的,下面是采用JavaBean+Servlet+JSP+DAO的Web架构设计的图书管理系统。
首先,我们需要建立一个图书Bean,包含以下字段:
```java
public class Book {
private int id;
private String name;
private String author;
private double price;
// 构造函数、getters 和 setters 略
}
```
接下来,我们可以编写一个简单的 DAO 类,使用 JDBC 来实现增删改查操作。例如:
```java
public class BookDAO {
public static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
public static final String DB_URL = "jdbc:mysql://localhost:3306/books";
public static final String USER = "root";
public static final String PASS = "password";
public List<Book> getAllBooks() throws SQLException {
List<Book> books = new ArrayList<>();
Connection conn = null;
Statement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
stmt = conn.createStatement();
String sql = "SELECT * FROM book";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
Book book = new Book();
book.setId(rs.getInt("id"));
book.setName(rs.getString("name"));
book.setAuthor(rs.getString("author"));
book.setPrice(rs.getDouble("price"));
books.add(book);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
}
return books;
}
public void addBook(Book book) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sql = "INSERT INTO book (name, author, price) VALUES (?, ?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setString(1, book.getName());
stmt.setString(2, book.getAuthor());
stmt.setDouble(3, book.getPrice());
stmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
}
}
public void updateBook(int id, Book book) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sql = "UPDATE book SET name=?, author=?, price=? WHERE id=?";
stmt = conn.prepareStatement(sql);
stmt.setString(1, book.getName());
stmt.setString(2, book.getAuthor());
stmt.setDouble(3, book.getPrice());
stmt.setInt(4, id);
stmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
}
}
public void deleteBook(int id) throws SQLException {
Connection conn = null;
PreparedStatement stmt = null;
try {
Class.forName(JDBC_DRIVER);
conn = DriverManager.getConnection(DB_URL, USER, PASS);
String sql = "DELETE FROM book WHERE id=?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, id);
stmt.executeUpdate();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (stmt != null) stmt.close();
if (conn != null) conn.close();
}
}
}
```
这个 DAO 类使用 MySQL 数据库来存储图书信息,提供了四个方法:
- getAllBooks:获取所有图书信息
- addBook:添加一本新书
- updateBook:更新一本图书的信息
- deleteBook:删除一本图书
接下来,我们可以编写一个 Servlet 类,处理 HTTP 请求并调用 DAO 类的方法来实现增删改查操作。例如:
```java
public class BookServlet extends HttpServlet {
private BookDAO dao = new BookDAO();
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Book> books = null;
try {
books = dao.getAllBooks();
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("books", books);
request.getRequestDispatcher("/book_list.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action = request.getParameter("action");
if ("add".equals(action)) {
String name = request.getParameter("name");
String author = request.getParameter("author");
double price = Double.parseDouble(request.getParameter("price"));
Book book = new Book();
book.setName(name);
book.setAuthor(author);
book.setPrice(price);
try {
dao.addBook(book);
} catch (SQLException e) {
e.printStackTrace();
}
} else if ("update".equals(action)) {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String author = request.getParameter("author");
double price = Double.parseDouble(request.getParameter("price"));
Book book = new Book();
book.setName(name);
book.setAuthor(author);
book.setPrice(price);
try {
dao.updateBook(id, book);
} catch (SQLException e) {
e.printStackTrace();
}
} else if ("delete".equals(action)) {
int id = Integer.parseInt(request.getParameter("id"));
try {
dao.deleteBook(id);
} catch (SQLException e) {
e.printStackTrace();
}
}
response.sendRedirect(request.getContextPath() + "/book");
}
}
```
这个 Servlet 类处理了 GET 和 POST 请求,GET 请求返回所有图书信息,POST 请求根据参数判断是添加、更新还是删除图书信息。
最后,我们可以编写一个 JSP 页面,展示所有图书信息,并提供表单来添加、更新和删除图书信息。例如:
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书管理系统</title>
</head>
<body>
<h1>图书列表</h1>
<table border="1">
<tr>
<th>图书号</th>
<th>图书名</th>
<th>作者</th>
<th>价格</th>
<th>操作</th>
</tr>
<c:forEach items="${books}" var="book">
<tr>
<td>${book.id}</td>
<td>${book.name}</td>
<td>${book.author}</td>
<td>${book.price}</td>
<td>
<form action="${pageContext.request.contextPath}/book" method="post">
<input type="hidden" name="id" value="${book.id}">
<input type="hidden" name="action" value="update">
<input type="text" name="name" value="${book.name}">
<input type="text" name="author" value="${book.author}">
<input type="text" name="price" value="${book.price}">
<input type="submit" value="更新">
</form>
<form action="${pageContext.request.contextPath}/book" method="post">
<input type="hidden" name="id" value="${book.id}">
<input type="hidden" name="action" value="delete">
<input type="submit" value="删除">
</form>
</td>
</tr>
</c:forEach>
</table>
<h1>添加图书</h1>
<form action="${pageContext.request.contextPath}/book" method="post">
<input type="hidden" name="action" value="add">
<label>图书名:</label><input type="text" name="name"><br>
<label>作者:</label><input type="text" name="author"><br>
<label>价格:</label><input type="text" name="price"><br>
<input type="submit" value="添加">
</form>
</body>
</html>
```
这个 JSP 页面展示了所有图书信息,并提供表单来添加、更新和删除图书信息。
最后,我们需要将这些组件组合在一起,例如在 web.xml 中配置 Servlet 和 JSP 的映射关系:
```xml
<servlet>
<servlet-name>BookServlet</servlet-name>
<servlet-class>com.example.BookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>BookServlet</servlet-name>
<url-pattern>/book</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>book.jsp</welcome-file>
</welcome-file-list>
```
这个配置将 BookServlet 映射到 /book 路径,将 book.jsp 设置为默认欢迎页面。
这样,我们就完成了采用JavaBean+Servlet+JSP+DAO的Web架构设计的图书管理系统。您可以使用 Tomcat 等 Web 服务器来部署和运行这个系统,然后在浏览器中访问 /book 路径来使用它。
阅读全文