查询,增加,删除,更新,分页功能
作者:JavaISoCool
来源:CSDN
原文:https://blog.csdn.net/javadamn/article/details/88026845
版权声明:本文为博主原创文章,转载请附上博文链接!
项目所需jar包
JSTL;C3P0;JDBC(MySql);dbUtils
数据库准备
CREATE DATABASE stus;
USE stus;
CREATE TABLE stu (
sid INT PRIMARY KEY AUTO_INCREMENT,
sname VARCHAR (20),
gender VARCHAR (5),
phone VARCHAR (20),
birthday DATE,
hobby VARCHAR(50),
info VARCHAR(200)
);
分页效果
添加
删除
模糊查询
更新
项目结构
整体结构
WebContent
Java Resources
具体代码
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>stu</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>StudentListServlet</display-name>
<servlet-name>StudentListServlet</servlet-name>
<servlet-class>com.itheima.servlet.StudentListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StudentListServlet</servlet-name>
<url-pattern>/StudentListServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>AddServlet</display-name>
<servlet-name>AddServlet</servlet-name>
<servlet-class>com.itheima.servlet.AddServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddServlet</servlet-name>
<url-pattern>/AddServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>DeleteServlet</display-name>
<servlet-name>DeleteServlet</servlet-name>
<servlet-class>com.itheima.servlet.DeleteServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DeleteServlet</servlet-name>
<url-pattern>/DeleteServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>EditServlet</display-name>
<servlet-name>EditServlet</servlet-name>
<servlet-class>com.itheima.servlet.EditServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EditServlet</servlet-name>
<url-pattern>/EditServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>UpdateServlet</display-name>
<servlet-name>UpdateServlet</servlet-name>
<servlet-class>com.itheima.servlet.UpdateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UpdateServlet</servlet-name>
<url-pattern>/UpdateServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>SearchStudentServlet</display-name>
<servlet-name>SearchStudentServlet</servlet-name>
<servlet-class>com.itheima.servlet.SearchStudentServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SearchStudentServlet</servlet-name>
<url-pattern>/SearchStudentServlet</url-pattern>
</servlet-mapping>
<servlet>
<description></description>
<display-name>StudentListPageServlet</display-name>
<servlet-name>StudentListPageServlet</servlet-name>
<servlet-class>com.itheima.servlet.StudentListPageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>StudentListPageServlet</servlet-name>
<url-pattern>/StudentListPageServlet</url-pattern>
</servlet-mapping>
</web-app>
WebContent目录下的具体代码
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>首页</title>
</head>
<body>
<h3><a href="StudentListServlet">显示所有学生列表</a></h3><br>
<h3><a href="StudentListPageServlet?currentPage=1">分页显示所有学生</a></h3>
</body>
</html>
add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加学生页面</title>
</head>
<body>
<h3>添加学生页面</h3>
<form method="post" action="AddServlet">
<table border="1" width="600">
<tr>
<td>姓名</td>
<td><input type="text" name="sname"></td>
</tr>
<tr>
<td>性别</td>
<td>
<input type="radio" name="gender" value="男">男
<input type="radio" name="gender" value="女">女
</td>
</tr>
<tr>
<td>电话</td>
<td><input type="text" name="phone"></td>
</tr>
<tr>
<td>生日</td>
<td><input type="text" name="birthday"></td>
</tr>
<tr>
<td>爱好</td>
<td>
<input type="checkbox" name="hobby" value="游泳">游泳
<input type="checkbox" name="hobby" value="篮球">篮球
<input type="checkbox" name="hobby" value="足球">足球
<input type="checkbox" name="hobby" value="看书">看书
<input type="checkbox" name="hobby" value="写字">写字
</td>
</tr>
<tr>
<td>简介</td>
<td><textarea name="info" rows="3" cols="20"></textarea></td>
</tr>
<tr>
<td colspan="2"> <input type="submit" value="添加"> </td>
</tr>
</table>
</form>
</body>
</html>
edit.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>更新学生页面</title>
</head>
<body>
<h3>更新学生页面</h3>
<form method="post" action="UpdateServlet">
<input type="hidden" name="sid" value="${stu.sid }">