JavaWeb实现查询功能
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
JavaWeb实现查询功能关于此次CRUD所需要的jar包,本⼈把⽂件放在了百度⽹盘,需要的⾃⾏去下载:
链接:
提取码: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="/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>${}</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>
<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>
<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应当有属于⾃⼰的风格。