Java web图书管理系统代码怎么写
时间: 2023-09-24 20:14:09 浏览: 104
java图书管理系统代码
这是一个比较大的项目,需要涉及到前端、后端和数据库等多个方面的知识。以下是一个简单的Java web图书管理系统的代码示例,仅供参考:
1. 创建数据库表
首先需要创建相应的数据库表,例如:
```
CREATE TABLE `book` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`author` varchar(255) NOT NULL,
`price` double NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
```
2. 编写JavaBean类
在Java中创建一个Book类,用于封装书籍的相关信息,例如:
```
public class Book {
private int id;
private String name;
private String author;
private double price;
// 省略get/set方法
}
```
3. 编写DAO类
创建一个BookDAO类,用于实现对数据库的增删改查操作,例如:
```
public class BookDAO {
// JDBC连接
private Connection conn = null;
// SQL语句执行器
private PreparedStatement pstmt = null;
// 结果集
private ResultSet rs = null;
// 构造方法中初始化数据库连接
public BookDAO() {
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/book", "root", "123456");
} catch (Exception e) {
e.printStackTrace();
}
}
// 添加书籍
public boolean add(Book book) {
try {
String sql = "INSERT INTO book(name, author, price) VALUES(?, ?, ?)";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, book.getName());
pstmt.setString(2, book.getAuthor());
pstmt.setDouble(3, book.getPrice());
int result = pstmt.executeUpdate();
if (result > 0) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
// 根据ID删除书籍
public boolean delete(int id) {
try {
String sql = "DELETE FROM book WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
int result = pstmt.executeUpdate();
if (result > 0) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
// 根据ID修改书籍信息
public boolean update(Book book) {
try {
String sql = "UPDATE book SET name=?, author=?, price=? WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, book.getName());
pstmt.setString(2, book.getAuthor());
pstmt.setDouble(3, book.getPrice());
pstmt.setInt(4, book.getId());
int result = pstmt.executeUpdate();
if (result > 0) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
// 根据ID查询书籍信息
public Book findById(int id) {
Book book = new Book();
try {
String sql = "SELECT * FROM book WHERE id=?";
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, id);
rs = pstmt.executeQuery();
if (rs.next()) {
book.setId(rs.getInt("id"));
book.setName(rs.getString("name"));
book.setAuthor(rs.getString("author"));
book.setPrice(rs.getDouble("price"));
}
} catch (Exception e) {
e.printStackTrace();
}
return book;
}
// 查询所有书籍信息
public List<Book> findAll() {
List<Book> list = new ArrayList<>();
try {
String sql = "SELECT * FROM book";
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
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"));
list.add(book);
}
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
// 关闭数据库连接
public void close() {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (conn != null) {
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
4. 编写Servlet类
创建一个BookServlet类,用于接收前端的请求并调用DAO类中的方法进行处理,例如:
```
@WebServlet("/book")
public class BookServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 获取请求参数
String action = request.getParameter("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"));
// 创建DAO对象
BookDAO dao = new BookDAO();
// 根据请求参数调用相应的方法
if (action.equals("add")) {
// 添加书籍
Book book = new Book();
book.setName(name);
book.setAuthor(author);
book.setPrice(price);
boolean result = dao.add(book);
if (result) {
response.getWriter().print("添加成功");
} else {
response.getWriter().print("添加失败");
}
} else if (action.equals("delete")) {
// 删除书籍
boolean result = dao.delete(id);
if (result) {
response.getWriter().print("删除成功");
} else {
response.getWriter().print("删除失败");
}
} else if (action.equals("update")) {
// 修改书籍信息
Book book = new Book();
book.setId(id);
book.setName(name);
book.setAuthor(author);
book.setPrice(price);
boolean result = dao.update(book);
if (result) {
response.getWriter().print("修改成功");
} else {
response.getWriter().print("修改失败");
}
} else if (action.equals("findById")) {
// 根据ID查询书籍信息
Book book = dao.findById(id);
response.getWriter().print(book);
} else if (action.equals("findAll")) {
// 查询所有书籍信息
List<Book> list = dao.findAll();
response.getWriter().print(list);
}
// 关闭数据库连接
dao.close();
}
}
```
5. 编写前端页面
最后,创建一个前端页面,用于向Servlet发送请求,并显示相应的结果。例如:
```
<!DOCTYPE html>
<html>
<head>
<title>图书管理系统</title>
<meta charset="UTF-8">
</head>
<body>
<form action="book" method="post">
<input type="hidden" name="action" value="add">
<p>书名:<input type="text" name="name"></p>
<p>作者:<input type="text" name="author"></p>
<p>价格:<input type="text" name="price"></p>
<p><input type="submit" value="添加"></p>
</form>
<form action="book" method="post">
<input type="hidden" name="action" value="delete">
<p>ID:<input type="text" name="id"></p>
<p><input type="submit" value="删除"></p>
</form>
<form action="book" method="post">
<input type="hidden" name="action" value="update">
<p>ID:<input type="text" name="id"></p>
<p>书名:<input type="text" name="name"></p>
<p>作者:<input type="text" name="author"></p>
<p>价格:<input type="text" name="price"></p>
<p><input type="submit" value="修改"></p>
</form>
<form action="book" method="post">
<input type="hidden" name="action" value="findById">
<p>ID:<input type="text" name="id"></p>
<p><input type="submit" value="查询"></p>
</form>
<form action="book" method="post">
<input type="hidden" name="action" value="findAll">
<p><input type="submit" value="查询所有"></p>
</form>
</body>
</html>
```
以上是一个简单的Java web图书管理系统的代码示例,仅供参考。实际开发中,需要根据具体需求进行修改和完善。
阅读全文