JavaWeb實現增刪查改(圖書信息管理)之查詢功能實現

 

 關於這次CRUD所須要的jar包,本人把文件放在了百度網盤,須要的自行去下載:html

 

  連接:https://pan.baidu.com/s/1Pqe88u6aPaeVjjOq1YFQ-w 
  提取碼:pimz java

數據庫使用的是SqlServer,開發工具使用IDEAsql

這次實現的是增刪查改,以圖書信息管理爲例,結構以下↓



接下來,就是項目,代碼:↓
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應當有屬於本身的風格__________________________________________________________________________________________________________________________________________jsp

相關文章
相關標籤/搜索