图书管理系统( JSP + JDBC + Servlet)实现-07:模糊查询功能
Posted Z && Y
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图书管理系统( JSP + JDBC + Servlet)实现-07:模糊查询功能相关的知识,希望对你有一定的参考价值。
01: 流程分析和数据库建表阶段
02: 编写和配置过滤器(防止页面乱码)
03:项目搭建 & 工具类的实现 &依赖导入
04: 建立数据库的实体类(pojo)
05: 实现登录功能
06: 查询所有书籍功能
07:模糊查询功能
08:页面所有功能实现(附源码下载)
2.1 实现-07: 模糊查询功能
后面这些步骤都跟前面类型,以后就不做过多叙述了。
2.1.1 BookDao
BookDao.java
public List<Book> getBookByName(Connection connection, String bookName) throws Exception;
}
2.1.1 BookDaoImpl
BookDaoImpl.java
@Override
public List<Book> getBookByName(Connection connection, String bookName) throws Exception {
PreparedStatement preparedStatement = null;
ResultSet rs = null;
Book book = null;
List<Book> books = new ArrayList<>();
if (null != connection) {
String sql = "select * from books where bookName like ?";
Object[] params = {"%" + bookName + "%"};
rs = BaseDao.executeQuery(connection, preparedStatement, rs, sql, params);
while (rs.next()) {
book = new Book();
book.setInventory(rs.getInt("inventory"));
book.setBookName(rs.getString("bookName"));
book.setBookId(rs.getString("bookId"));
book.setAuthor(rs.getString("author"));
books.add(book);
}
BaseDao.closeResource(connection, preparedStatement, rs);
}
return books;
}
2.1.3 创建 BookService
BookService.java
public List<Book> getBookByName(String bookName) throws Exception;
2.1.4 创建 BookServiceImpl
BookServiceImpl.java
@Override
public List<Book> getBookByName(String bookName) throws Exception {
Connection connection = BaseDao.getConnection();
return new BookDaoImpl().getBookByName(connection, bookName);
}
2.1.5 创建 GetBookByNameServlet
GetBookByNameServlet.jsp
package com.tian.servlet.book;
import com.tian.pojo.Book;
import com.tian.service.book.BookServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
* ClassName: GetBookByNameServlet
* Description:
*
* @author Tianjiao
* @date 2021/5/29 23:45
*/
public class GetBookByNameServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 获取前端传递过来的书籍的信息
String bookName = req.getParameter("bookName");
BookServiceImpl bookService = new BookServiceImpl();
try {
List<Book> books = bookService.getBookByName(bookName);
req.setAttribute("books", books);
req.getRequestDispatcher("static/jsp/index.jsp").forward(req, resp);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
2.1.6 配置 GetBookByNameServlet
web.xml
<servlet>
<servlet-name>GetBookByNameServlet</servlet-name>
<servlet-class>com.tian.servlet.book.GetBookByNameServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>GetBookByNameServlet</servlet-name>
<url-pattern>/getBooksByName</url-pattern>
</servlet-mapping>
2.1.7 启动服务器,测试
以上是关于图书管理系统( JSP + JDBC + Servlet)实现-07:模糊查询功能的主要内容,如果未能解决你的问题,请参考以下文章
图书管理系统( ( JSP + JDBC + Servlet ) )实现-06: 查询所有书籍功能
图书管理系统( JSP + JDBC + Servlet)实现-07:模糊查询功能
图书管理系统( JSP + JDBC + Servlet )实现-08:页面所有功能实现
图书管理系统( ( JSP + JDBC + Servlet ) )实现-04: 建立数据库的实体类(pojo)