jsp+servlet+mysql图书管理代码示例
时间: 2023-08-27 13:06:59 浏览: 133
图书管理系统-jsp+servlet+mysql
以下是一个简单的图书管理系统的 JSP + Servlet + MySQL 示例代码:
1. 数据库表设计
创建一张名为 book 的表,包含以下字段:
| 字段名 | 类型 | 描述 |
| ------ | -------- | -------- |
| id | int(11) | 主键 |
| name | varchar | 书名 |
| author | varchar | 作者名 |
| price | decimal | 价格 |
| date | datetime | 出版日期 |
2. JSP 页面设计
创建一个名为 index.jsp 的页面,包含以下内容:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书管理系统</title>
</head>
<body>
<h1>图书管理系统</h1>
<form action="search" method="post">
<label for="name">书名:</label>
<input type="text" name="name" id="name">
<input type="submit" value="搜索">
</form>
<table>
<thead>
<tr>
<th>ID</th>
<th>书名</th>
<th>作者</th>
<th>价格</th>
<th>出版日期</th>
</tr>
</thead>
<tbody>
<c:forEach var="book" items="${books}">
<tr>
<td>${book.id}</td>
<td>${book.name}</td>
<td>${book.author}</td>
<td>${book.price}</td>
<td>${book.date}</td>
</tr>
</c:forEach>
</tbody>
</table>
<a href="add.jsp">添加图书</a>
</body>
</html>
```
3. Servlet 实现
创建一个名为 SearchServlet 的 Servlet,用于处理搜索请求和显示图书列表,包含以下代码:
```java
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/search")
public class SearchServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
List<Book> books = new ArrayList<>();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore?useSSL=false", "root", "password");
PreparedStatement stmt;
if (name != null && !name.isEmpty()) {
stmt = conn.prepareStatement("SELECT * FROM book WHERE name LIKE ?");
stmt.setString(1, "%" + name + "%");
} else {
stmt = conn.prepareStatement("SELECT * FROM book");
}
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
int id = rs.getInt("id");
String bookName = rs.getString("name");
String author = rs.getString("author");
double price = rs.getDouble("price");
String date = rs.getString("date");
Book book = new Book(id, bookName, author, price, date);
books.add(book);
}
conn.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
request.setAttribute("books", books);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
```
4. 添加图书页面设计
创建一个名为 add.jsp 的页面,包含以下内容:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加图书</title>
</head>
<body>
<h1>添加图书</h1>
<form action="add" method="post">
<label for="name">书名:</label>
<input type="text" name="name" id="name"><br>
<label for="author">作者:</label>
<input type="text" name="author" id="author"><br>
<label for="price">价格:</label>
<input type="text" name="price" id="price"><br>
<label for="date">出版日期:</label>
<input type="text" name="date" id="date"><br>
<input type="submit" value="添加">
</form>
<a href="index.jsp">返回首页</a>
</body>
</html>
```
5. 添加图书 Servlet 实现
创建一个名为 AddServlet 的 Servlet,用于处理添加图书请求,包含以下代码:
```java
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/add")
public class AddServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String author = request.getParameter("author");
double price = Double.parseDouble(request.getParameter("price"));
String date = request.getParameter("date");
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore?useSSL=false", "root", "password");
PreparedStatement stmt = conn.prepareStatement("INSERT INTO book (name, author, price, date) VALUES (?, ?, ?, ?)");
stmt.setString(1, name);
stmt.setString(2, author);
stmt.setDouble(3, price);
stmt.setString(4, date);
stmt.executeUpdate();
conn.close();
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
response.sendRedirect("index.jsp");
}
}
```
以上就是一个简单的图书管理系统的 JSP + Servlet + MySQL 示例代码。
阅读全文