JavaWeb实现查询功能


 关于此次CRUD所需要的jar包,本人把文件放在了百度网盘,需要的自行去下载:


  链接:https://pan.baidu.com/s/1Pqe88u6aPaeVjjOq1YFQ-w 
  提取码:pimz 

数据库使用的是SqlServer,开发工具使用IDEA

此次实现的是增删查改,以图书信息管理为例,结构如下↓



接下来,就是项目,代码:↓
index.jsp
<%@ page import="java.util.List" %>
<%@ page import="BookSystem.Other.Books" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="root" value="${pageContext.request.contextPath}" scope="page"/>
<html>
  <head>
    <title>图书管理系统主页</title>
    <style>
      body{
       background-image: url("/img/1.jpg");
       background-repeat: no-repeat;
      }
      table{
        text-align: center;
      }

    </style>

  </head>
  <body >
<%--使用jstl格式--%>
  <h2>-----------------------书籍信息列表------------------------</h2>
  <br>

  <section>
  <table border="1"  cellspacing="0"
         cellpadding="0"  width="600" height="200" >
      <%--标题--%>
            <th>编号</th>
            <th>书名</th>
            <th>作者</th>
            <th>库存</th>
            <th>价格</th>
            <th>出版社</th>
            <th>操作</th>


        <c:forEach var="book1" varStatus="s" items="${aaa}">
          <tr>

            <td>${book1.id}</td>
            <td>${book1.name}</td>
            <td>${book1.author}</td>
            <td>${book1.number}</td>
            <td>${book1.price}</td>
            <td>${book1.pub}</td>
            <td>
              <a href="${root}/books/del?id=${book1.id}">删除</a>
              &nbsp;
              <a href="${root}/books/update?id=${book1.id}">修改</a>
            </td>

          </tr>
        </c:forEach>

        <c:if test="${empty aaa}">
          <tr>
            <td colspan="9">没有任何书籍,可以点击选择<a href="${root}/books/add"> 这里 </a>添加书籍</td>
          </tr>
        </c:if>

      </table>
    </section>

  <br />

  <section>
    &nbsp;
<a href="<%=request.getContextPath()%>/books/add">添加书籍信息</a>
  </section>



  <br>
  <h2>-----------------------------------------------------------</h2>

  </body>

</html>

对应的servlet——bookList.java↓

package BookSystem.CRUD;

import BookSystem.Other.Books;
import BookSystem.Other.DButil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/books/lst")
public class BookList extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Books> books = new ArrayList<>();
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = new DButil().getConnection();
            stmt = conn.createStatement();
            rs = stmt.executeQuery("select book_id, book_name, author, number , price , pub from BookInfo");

            while (rs.next()) {
                Books books1 = new Books(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getInt(4),rs.getFloat(5),rs.getString(6));
                books.add(books1);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {

            DButil.close(conn, stmt, rs);
        }


        req.setAttribute("aaa", books);
        req.getRequestDispatcher("/Book/index.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

      注:该整个CRUD不展示效果图,整体CSS应当有属于自己的风格__________________________________________________________________________________________________________________________________________

 
原文地址:https://www.cnblogs.com/winton-nfs/p/11457943.html