用JSP完成简单的图书查询系统代码
时间: 2023-12-22 17:04:34 浏览: 222
chapter11_JSP源码_图书管理系统_
好的,以下是一个简单的JSP图书查询系统的代码实现,包括JSP页面、Servlet和数据库连接的代码:
1. JSP页面(book.jsp):
```jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>图书查询系统</title>
</head>
<body>
<h1>图书查询系统</h1>
<form action="BookServlet" method="get">
<label>请输入关键字:</label>
<input type="text" name="keyword">
<input type="submit" value="查询">
</form>
<hr>
<h2>查询结果:</h2>
<table border="1">
<tr>
<th>图书ID</th>
<th>图书名称</th>
<th>图书作者</th>
<th>图书价格</th>
<th>出版社</th>
<th>出版日期</th>
</tr>
<c:forEach var="book" items="${books}">
<tr>
<td>${book.book_id}</td>
<td>${book.book_name}</td>
<td>${book.book_author}</td>
<td>${book.book_price}</td>
<td>${book.book_publisher}</td>
<td>${book.book_pubdate}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
```
2. Servlet(BookServlet.java):
```java
package com.example.servlet;
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.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.example.bean.Book;
public class BookServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取关键字
String keyword = request.getParameter("keyword");
// 查询图书
List<Book> books = search(keyword);
// 存储查询结果
request.setAttribute("books", books);
// 转发到JSP页面
request.getRequestDispatcher("book.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
// 查询图书
private List<Book> search(String keyword) {
List<Book> books = new ArrayList<Book>();
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
// 加载MySQL驱动程序
Class.forName("com.mysql.jdbc.Driver");
// 获取数据库连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
// SQL语句
String sql = "select * from book where book_name like ?";
// 创建PreparedStatement对象
stmt = conn.prepareStatement(sql);
// 设置参数
stmt.setString(1, "%" + keyword + "%");
// 执行查询
rs = stmt.executeQuery();
// 处理查询结果
while (rs.next()) {
Book book = new Book();
book.setBookId(rs.getInt("book_id"));
book.setBookName(rs.getString("book_name"));
book.setBookAuthor(rs.getString("book_author"));
book.setBookPrice(rs.getDouble("book_price"));
book.setBookPublisher(rs.getString("book_publisher"));
book.setBookPubdate(rs.getDate("book_pubdate"));
books.add(book);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 释放资源
try {
if (rs != null)
rs.close();
if (stmt != null)
stmt.close();
if (conn != null)
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return books;
}
}
```
3. Bean(Book.java):
```java
package com.example.bean;
import java.util.Date;
public class Book {
private int bookId;
private String bookName;
private String bookAuthor;
private double bookPrice;
private String bookPublisher;
private Date bookPubdate;
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookAuthor() {
return bookAuthor;
}
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
public double getBookPrice() {
return bookPrice;
}
public void setBookPrice(double bookPrice) {
this.bookPrice = bookPrice;
}
public String getBookPublisher() {
return bookPublisher;
}
public void setBookPublisher(String bookPublisher) {
this.bookPublisher = bookPublisher;
}
public Date getBookPubdate() {
return bookPubdate;
}
public void setBookPubdate(Date bookPubdate) {
this.bookPubdate = bookPubdate;
}
}
```
以上是一个简单的JSP图书查询系统的代码实现,您可以根据自己的需求进行修改和完善。需要注意的是,为了便于演示,本实现中使用了JDBC连接数据库,实际开发中应该使用连接池。
阅读全文