实现目标:![](https://img-blog.csdnimg.cn/20210814042532831.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3h5eV8wNTE3,size_16,color_FFFFFF,t_70)
第一步:建表
第二步:界面
首页显示:
首页数据加载:
/**
*首页数据加载
* @author zjjt
*
*/
@WebServlet("/index.do")
public class indexservlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
req.setCharacterEncoding("utf-8");
String key = req.getParameter("stuclass1");
String key1 = req.getParameter("teacher1");
String[] key2 = req.getParameterValues("id");
String hobby1 = "";
if (key2 != null && key2.length > 0) {
for (String string : key2) {
hobby1 += string;
}
}
if (key == null||key.equals("请选择"))
key = "";
if (key1 == null||key1.equals("请选择"))
key1 = "";
Studentdao s = new Studentdao();
List<StuClass> stuclass = s.getallclass();
List<Teacher> teacher = s.getallcher();
List<Hobby> hobby = s.getallhobby();
String pid = req.getParameter("pid");
int pages = 1;
if (pid != null) {
pages = Integer.parseInt(pid);
}
int pagesSize = 3;
int begin = (pages - 1) * pagesSize + 1;
int end = pages * pagesSize;
int row = s.getrow();
int max = (int) Math.ceil(row * 1.0 / pagesSize);
List<Student> student = s.getall(key1, key, hobby1, begin, end);
req.setAttribute("stuclass", stuclass);
req.setAttribute("teacher", teacher);
req.setAttribute("hobby", hobby);
req.setAttribute("student", student);
req.setAttribute("max", max);
req.setAttribute("pages", pages);
req.setAttribute("row", row);
req.getRequestDispatcher("index.jsp").forward(req, resp);
}
}
首页数据显示:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<script src="js/jquery-3.5.1.js"></script>
<style type="text/css">
table,tr {
border-collapse: collapse;
border-color: blue;
}
td{
text-align: center;
}
</style>
<script>
var luy="";
$({
$('input:checkbox[name="id"]:checked').each(function(){
luy=luy+$(this).val()
})
console.log(luy)
})
</script>
<body><form action="">
<table border>
<tr >
<td colspan="6">老师:
<select name="teacher1">
<option>请选择</option>
<c:forEach items="${teacher }" var="t">
<option value="${t.teacher}">${t.teacher}</option>
</c:forEach>
</select>
班级:
<select style="margin-left: 40px" name="stuclass1">
<option>请选择</option>
<c:forEach items="${stuclass}" var="s">
<option value="${s.name}">${s.name}</option>
</c:forEach>
</select>
爱好:
<c:forEach items="${hobby }" var="h">
<input type="checkbox" value="${h.hooby}" name="id"/>${h.hooby}
</c:forEach>
<button onclick="location.href='index.do'">搜索</button>
<a href="add.do">新增</a>
</td>
</tr>
<tr>
<td>学生ID</td> <td>学生姓名</td>
<td>学生教员</td> <td>学生所在班级</td>
<td>学生爱好</td> <td>操作</td>
</tr>
<c:forEach items="${student}" var="stu">
<tr>
<td>${stu.sid }</td> <td>${stu.name }</td>
<td>${stu.teacher}</td> <td>${stu.classid}</td>
<td>${stu.hobby}</td> <td><a href="delstu.do?id=${stu.sid }">删除</a> <a href="updstu.do?id=${stu.sid }">修改</a></td>
</tr>
</c:forEach>
<tr><td colspan="6">第${pages}页 共${max}页 共${row}条 <a href="index.do?pid=1">首页</a>
<a href="index.do?pid=${pages-1<1?1:pages-1}">上一页</a> <a href="index.do?pid=${pages+1>max?max:pages+1 }">下一页</a> <a href="index.do?pid=${max}">尾页</a>
</td></tr>
</table>
</form>
</body>
</html>
增加界面显示:
增加界面数据加载:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
req.setCharacterEncoding("utf-8");
String name = req.getParameter("name");
String teacher = req.getParameter("teacher");
String stuclass = req.getParameter("stuclass");
String[] hobby = req.getParameterValues("hobby");
String hobby1 = "";
if (hobby != null && hobby.length > 0) {
for (String string : hobby) {
hobby1 += string + ",";
}
}
Studentdao studao = new Studentdao();
Student s = new Student();
s.setSid(studao.getmaxid());
s.setName(name);
s.setTeacher(teacher);
s.setClassid(stuclass);
s.setHobby(hobby1);
int addstudent = studao.addstudent(s);
resp.sendRedirect("index.do");
}
增加界面数据显示:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>增加界面</title>
</head>
<style type="text/css">
table,tr {
border-collapse: collapse;
border-color: blue;
}
td{
text-align: center;
}
</style>
<body>
<form action="addstudent.do">
<table border="">
<tr><td><p>名字:<input name="name"/></p></td></tr>
<tr><td><p>教员:<select name="teacher">
<option>请选择</option>
<c:forEach items="${teacher }" var="t">
<option value="${t.teacher}">${t.teacher}</option>
</c:forEach>
</select></p></td></tr>
<tr><td><p>班级:<select name="stuclass">
<option>请选择</option>
<c:forEach items="${stuclass}" var="s">
<option value="${s.name}">${s.name}</option>
</c:forEach>
</select></p></td></tr>
<tr><td><p>爱好:
<c:forEach items="${hobby }" var="h">
<input type="checkbox" value="${h.hooby}" name="hobby"/>${h.hooby}
</c:forEach>
</p></td></tr>
<tr><td><button>确认增加</button></td></tr>
</table>
</form>
</body>
</html>
修改界面显示:
修改界面数据加载:
1:把要修改的学生数据加载到界面上
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
req.setCharacterEncoding("utf-8");
String id = req.getParameter("id");
Studentdao s = new Studentdao();
Student student = s.getstudent(Integer.parseInt(id));
List<StuClass> stuclass = s.getallclass();
List<Teacher> teacher = s.getallcher();
List<Hobby> hobby = s.getallhobby();
String[] split = student.getHobby().split(",");
req.setAttribute("split", split);
req.setAttribute("student", student);
req.setAttribute("stuclass", stuclass);
req.setAttribute("teacher", teacher);
req.setAttribute("hobby", hobby);
req.getRequestDispatcher("upd.jsp").forward(req, resp);
2:修改学生数据:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
req.setCharacterEncoding("utf-8");
int id = Integer.parseInt(req.getParameter("id"));
String name = req.getParameter("name");
String teacher = req.getParameter("teacher");
String stuclass = req.getParameter("stuclass");
String[] hobby = req.getParameterValues("hobby");
String hobby1 = "";
if (hobby != null && hobby.length > 0) {
for (String string : hobby) {
hobby1 += string + ",";
}
}
Student s = new Student(id, name, teacher, stuclass, hobby1);
Studentdao dao = new Studentdao();
int n = dao.updstudent(s);
resp.sendRedirect("index.do");
}
修改界面数据显示:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改界面</title>
</head>
<style type="text/css">
table, tr {
border-collapse: collapse;
border-color: blue;
}
td {
text-align: center;
}
</style>
<body>
<form action="stupd.do">
<table border="">
<tr>
<td><p>
名字:<input name="name" value="${student.name}" />
</p></td>
</tr>
<tr>
<td><p>
教员:<select name="teacher">
<option>请选择</option>
<c:forEach items="${teacher }" var="t">
<option ${student.teacher==t.teacher?"selected":""}
value="${t.teacher}">${t.teacher}</option>
</c:forEach>
</select>
</p></td>
</tr>
<tr>
<td><p>
班级:<select name="stuclass">
<option>请选择</option>
<c:forEach items="${stuclass}" var="s">
<option ${student.classid==s.name?"selected":"" }
value="${s.name}">${s.name}</option>
</c:forEach>
</select>
</p></td>
</tr>
<tr>
<td><p>
爱好:
<c:forEach items="${hobby }" var="h">
<input type="checkbox" value="${h.hooby}" name="hobby"
<c:forEach items="${split }" var="sp">
<c:if test="${sp==h.hooby }">checked</c:if>
</c:forEach> />${h.hooby}
</c:forEach>
</p></td>
</tr>
<tr>
<td><button>确认修改</button> <input name="id"
value="${student.sid }" hidden="" /></td>
</tr>
</table>
</form>
</body>
</html>
删除的数据加载:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
req.setCharacterEncoding("utf-8");
String id = req.getParameter("id");
Studentdao studao = new Studentdao();
int delstudent = studao.delstudent(Integer.parseInt(id));
resp.sendRedirect("index.do");
}
3,dao方法
public class Studentdao implements Istudao{
Connection con;
PreparedStatement ps;
ResultSet rs;
/**
* 增加
*/
public int addstudent(Student s) {
con=DBHelper.getCon();
String sql="INSERT INTO tb_student VALUES(?,?,?,?,?)";
try {
ps=con.prepareStatement(sql);
ps.setInt(1, s.getSid());
ps.setString(2, s.getName());
ps.setString(3, s.getTeacher());
ps.setString(4, s.getClassid());
ps.setString(5, s.getHobby());
return ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return 0;
}
/**
* 设置学生id
*/
public int getmaxid() {
con=DBHelper.getCon();
String sql="select nvl(max(sid),0)+1 from tb_student";
try {
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
if(rs.next()) {
return rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return 0;
}
/**
* 删除
*/
public int delstudent(int id) {
con=DBHelper.getCon();
String sql="DELETE FROM tb_student WHERE SID=? ";
try {
ps=con.prepareStatement(sql);
ps.setInt(1, id);
return ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return 0;
}
/**
* 修改
*/
public int updstudent(Student s) {
con=DBHelper.getCon();
String sql="UPDATE tb_student SET NAME=?,teacher=?,classid=?,hobby=? WHERE SID=?";
try {
ps=con.prepareStatement(sql);
ps.setString(1, s.getName());
ps.setString(2, s.getTeacher());
ps.setString(3, s.getClassid());
ps.setString(4, s.getHobby());
ps.setInt(5, s.getSid());
return ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return 0;
}
/**
* 查询 &&模糊查询 &&分页
*/
public List<Student> getall(String teacher, String Class, String hobby, int begin, int end) {
List<Student> list=new ArrayList<Student>();
con=DBHelper.getCon();
String sql="SELECT * FROM( SELECT r.* ,ROWNUM rid FROM tb_student r WHERE tname LIKE ? AND cname LIKE ? AND hname LIKE ? )t WHERE rid BETWEEN ? AND ? ";
try {
ps=con.prepareStatement(sql);
ps.setString(1, "%"+teacher+"%");
ps.setString(2, "%"+Class+"%");
ps.setString(3,"%"+hobby+"%");
ps.setInt(4, begin);
ps.setInt(5, end);
rs=ps.executeQuery();
while(rs.next()) {
Student s=new Student();
s.setSid(rs.getInt(1));
s.setName(rs.getString(2));
s.setTeacher(rs.getString(3));
s.setClassid(rs.getString(4));
s.setHobby(rs.getString(5));
list.add(s);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return list;
}
/**
* 查询单个
*/
public Student getstudent(int id) {
con=DBHelper.getCon();
String sql="select * from tb_student where sid=?";
try {
ps=con.prepareStatement(sql);
ps.setInt(1, id);
rs=ps.executeQuery();
if(rs.next()) {
Student s=new Student();
s.setSid(rs.getInt(1));
s.setName(rs.getString(2));
s.setTeacher(rs.getString(3));
s.setClassid(rs.getString(4));
s.setHobby(rs.getString(5));
return s;
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return null;
}
/**
* 获取所有班级
*/
public List<StuClass> getallclass() {
List<StuClass> list=new ArrayList<StuClass>();
con=DBHelper.getCon();
String sql="select * from tb_class";
try {
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()) {
StuClass s=new StuClass();
s.setClassid(rs.getInt(1));
s.setName(rs.getString(2));
list.add(s);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return list;
}
/**
* 获取所有老师
*/
public List<Teacher> getallcher() {
List<Teacher> list=new ArrayList<Teacher>();
con=DBHelper.getCon();
String sql="select * from tb_teacher";
try {
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()) {
Teacher s=new Teacher();
s.setTeacherid(rs.getInt(1));
s.setTeacher(rs.getString(2));
list.add(s);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return list;
}
/**
* 获取所有爱好
*/
public List<Hobby> getallhobby() {
List<Hobby> list=new ArrayList<Hobby>();
con=DBHelper.getCon();
String sql="SELECT * FROM tb_hobby";
try {
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()) {
Hobby s=new Hobby();
s.setHobbyid(rs.getInt(1));
s.setHooby(rs.getString(2));
list.add(s);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return list;
}
/**
* 获取学生表数据条数
*/
public int getrow() {
con=DBHelper.getCon();
String sql="select count(1) from tb_student";
try {
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
if(rs.next()) {
return rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
}