图书管理系统( ( JSP + JDBC + Servlet ) )实现-06: 查询所有书籍功能
Posted Z && Y
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图书管理系统( ( JSP + JDBC + Servlet ) )实现-06: 查询所有书籍功能相关的知识,希望对你有一定的参考价值。
01: 流程分析和数据库建表阶段
02: 编写和配置过滤器(防止页面乱码)
03:项目搭建 & 工具类的实现 &依赖导入
04: 建立数据库的实体类(pojo)
05: 实现登录功能
06: 查询所有书籍功能
07:模糊查询功能
08:页面所有功能实现(附源码下载)
2.0 实现-06: 查询所有书籍功能
刚刚页面跳转过去以后,页面并没有值,现在我们在跳转过去的时候,就从后台数据库取值
2.0.1 创建 BookDao
BookDao.java
package com.tian.dao.book;
import com.tian.pojo.Book;
import java.sql.Connection;
import java.util.List;
/**
* ClassName: BookDao
* Description: books表的dao层
*
* @author Tianjiao
* @date 2021/5/28 14:26
*/
public interface BookDao {
/**
* MethodName: getAllBook
* Description: 查询所有的书籍
*/
public List<Book> getAllBook(Connection connection) throws Exception;
}
2.0.1 创建 BookDaoImpl
BookDaoImpl.java
package com.tian.dao.book;
import com.tian.pojo.Book;
import com.tian.utils.BaseDao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
/**
* ClassName: BookDaoImpl
* Description: BookDao的实现类
*
* @author Tianjiao
* @date 2021/5/28 14:27
*/
public class BookDaoImpl implements BookDao {
@Override
public List<Book> getAllBook(Connection connection) throws Exception {
PreparedStatement preparedStatement = null;
ResultSet rs = null;
List<Book> books = new ArrayList<>();
Book book = null;
if (null != connection) {
String sql = "select * from `books`";
Object[] params = {};
rs = BaseDao.executeQuery(connection, preparedStatement, rs, sql, params);
int i = 0;
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.0.3 创建 BookService
BookService.java
package com.tian.service.book;
import com.tian.pojo.Book;
import java.util.List;
/**
* ClassName: BookService
* Description: Books的service层
*
* @author Tianjiao
* @date 2021/5/28 14:51
*/
public interface BookService {
/**
* MethodName: getAllBooks
* Description: 获得所有的书籍
*/
public List<Book> getAllBooks() throws Exception;
}
2.0.4 创建 BookServiceImpl
BookServiceImpl.java
package com.tian.service.book;
import com.tian.dao.book.BookDaoImpl;
import com.tian.pojo.Book;
import com.tian.utils.BaseDao;
import java.sql.Connection;
import java.util.List;
/**
* ClassName: BookServiceImpl
* Description: BookService的实现类
*
* @author Tianjiao
* @date 2021/5/28 14:51
*/
public class BookServiceImpl implements BookService {
@Override
public List<Book> getAllBooks() throws Exception {
Connection connection = BaseDao.getConnection();
return new BookDaoImpl().getAllBook(connection);
}
}
2.0.5 创建 AllBookServlet
AllBookServlet.jsp
package com.tian.servlet.book;
import com.tian.pojo.Book;
import com.tian.service.book.BookServiceImpl;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
* ClassName: BookServlet
* Description: 查询所有书籍的请求
*
* @author Tianjiao
* @date 2021/5/29 10:30
*/
public class AllBookServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
try {
List<Book> books = new BookServiceImpl().getAllBooks();
if (books != null) {
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 EnumConstantNotPresentException {
this.doGet(req, resp);
}
}
2.0.6 配置 AllBookServlet
web.xml
<servlet>
<servlet-name>AllBookServlet</servlet-name>
<servlet-class>com.tian.servlet.book.AllBookServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AllBookServlet</servlet-name>
<url-pattern>/books</url-pattern>
</servlet-mapping>
2.0.7 修改 LoginServlet
修改 LoginServlet 使其在通过 /books 请求进入index.jsp页面
2.0.8 启动服务器,测试
以上是关于图书管理系统( ( JSP + JDBC + Servlet ) )实现-06: 查询所有书籍功能的主要内容,如果未能解决你的问题,请参考以下文章
图书管理系统( ( JSP + JDBC + Servlet ) )实现-06: 查询所有书籍功能
图书管理系统( JSP + JDBC + Servlet)实现-07:模糊查询功能
图书管理系统( JSP + JDBC + Servlet )实现-08:页面所有功能实现
图书管理系统( ( JSP + JDBC + Servlet ) )实现-04: 建立数据库的实体类(pojo)