主界面
?新增界面
?添加成功之后会跳出一个提示款然后点击确定就会跳到主界面去
?
?修改界面
?
修改成功跟增加成功一样点击确定就会跳到主界面去
?
?点击删除会跳出一个提示框,确定要删除吗
同理,删除成功之后也会跳到主界面
Jsp: 主界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<!-- 引入Bootstrap的CSS -->
<link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
<!-- 引入jQuery的库 -->
<script type="text/javascript" src="js/jquery-3.6.0.js"></script>
<!-- 引入Bootstrap的库 -->
<script type="text/javascript" src="js/bootstrap.js"></script>
</head>
<body>
<br>
<div align="center">
<h2><a href='DoAddSerevelt'>新增</a></h2>
<div style="width: 60%;">
<form action="" method="post" class="form-inline">
<select class="form-control" name="tid">
<option value ="">请选择教员</option>
<c:forEach items="${tea }" var="t">
<option value ="${t.tid }">${t.tname }</option>
</c:forEach>
</select>
<select class="form-control" name="cid">
<option value ="">请选择班级</option>
<c:forEach items="${cla }" var="c">
<option value ="${c.cid }">${c.cname }</option>
</c:forEach>
</select>
学生爱好:
篮球 <input name="slike" type="checkbox" value="篮球">
足球 <input name="slike" type="checkbox" value="足球">
唱歌 <input name="slike" type="checkbox" value="唱歌">
跳舞 <input name="slike" type="checkbox" value="跳舞">
学生姓名:<input type="text" name="sname"/>
<button type="submit" class="btn btn-info">查询</button>
</form>
<table class="table table-bordered">
<tr>
<td>学生ID</td>
<td>学生姓名</td>
<td>学生教员</td>
<td>学生所在班级</td>
<td>学生爱好</td>
<td>操作</td>
</tr>
<c:forEach items="${stu }" var="s">
<tr>
<td>${s.sid }</td>
<td>${s.sname }</td>
<td>${s.tid.tname }</td>
<td>${s.cid.cname }</td>
<td>${s.slike }</td>
<td>
<a href="DoUpdateServlet?sid=${s.sid }">修改</a>
<a onclick="return confirm('确定要删除吗?')" href="DelServlet?sid=${s.sid }">删除</a>
</td>
</tr>
</c:forEach>
</table>
</div>
</div>
</body>
</html>
班级根据班级id查询和查询全部
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import entity.Class;
import util.DbHelp;
public class CDao {
/**
*查询单个班级对象
* @param cid 班级编号
* @return 返回班级对象
*/
public Class getid(int cid) {
Connection con=null;
PreparedStatement ps=null;
Class cla=null;
ResultSet rs=null;
try {
con=DbHelp.con();
ps=con.prepareStatement("select * from Class where cid="+cid);
rs=ps.executeQuery();
while(rs.next()) {
cla=new Class(rs.getInt("cid"), rs.getString("cname"));
}
}catch (Exception e) {
e.printStackTrace();
}finally {
DbHelp.close(con, ps, rs);
}
return cla;
}
/**
* 查询所有的班级
* @return 返回所有的班级集合
*/
public ArrayList<Class> getAll(){
Connection con=null;
PreparedStatement ps=null;
Class cla=null;
ResultSet rs=null;
ArrayList<Class> ar=new ArrayList<>();
try {
con=DbHelp.con();
ps=con.prepareStatement("select * from Class");
rs=ps.executeQuery();
while(rs.next()) {
cla=new Class(rs.getInt("cid"), rs.getString("cname"));
ar.add(cla);
}
}catch (Exception e) {
e.printStackTrace();
}finally {
DbHelp.close(con, ps, rs);
}
return ar;
}
}
?学生是根据id查询和班级id、学生的名字、教员id和学生的爱好查询
public Student getid(int sid) {
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
Student s=null;
try {
con=DbHelp.con();
ps=con.prepareStatement("select * from student where sid="+sid);
rs=ps.executeQuery();
if(rs.next()) {
s=new Student(rs.getInt("sid"), rs.getString("sname"), new TDao().getid(rs.getInt("tid")), new CDao().getid(rs.getInt("cid")), rs.getString("slike"));
}
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally {
DbHelp.close(con, ps, rs);
}
return s;
}
public ArrayList<Student> getLike(String sname,int tid,int cid,String slike){
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
Student ss=null;
ArrayList<Student> ar=new ArrayList<>();
try {
con=DbHelp.con();
String sql="select * from student where 1=1";
if(sname!=null) {
sql+=" and sname like '%"+sname+"%'";
}
if(tid!=0) {
sql+=" and tid="+tid;
}
if(cid!=0) {
sql+=" and cid="+cid;
}
if(slike!=null) {
sql+=" and slike like '%"+slike+"%'";
}
System.out.println(sql);
ps=con.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()) {
ss=new Student(rs.getInt("sid"), rs.getString("sname"), new TDao().getid(rs.getInt("tid")), new CDao().getid(rs.getInt("cid")), rs.getString("slike"));
ar.add(ss);
}
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}finally {
DbHelp.close(con, ps, rs);
}
return ar;
}
?教员是根据id查询和查询全部
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import entity.Techar;
import util.DbHelp;
public class TDao {
/**
* 查询单个
* @param tid 教员编号
* @return 返回教员对象
*/
public Techar getid(int tid) {
Connection con=null;
PreparedStatement ps=null;
Techar tea=null;
ResultSet rs=null;
try {
con=DbHelp.con();
ps=con.prepareStatement("select * from Techer where tid="+tid);
rs=ps.executeQuery();
while(rs.next()) {
tea=new Techar(rs.getInt("tid"), rs.getString("tname"));
}
}catch (Exception e) {
e.printStackTrace();
}finally {
DbHelp.close(con, ps, rs);
}
return tea;
}
/**
* 查询所有的教员
* @return 返回所有的教员集合
*/
public ArrayList<Techar> getAll(){
Connection con=null;
PreparedStatement ps=null;
Techar tea=null;
ResultSet rs=null;
ArrayList<Techar> ar=new ArrayList<>();
try {
con=DbHelp.con();
ps=con.prepareStatement("select * from Techer");
rs=ps.executeQuery();
while(rs.next()) {
tea=new Techar(rs.getInt("tid"), rs.getString("tname"));
ar.add(tea);
}
}catch (Exception e) {
e.printStackTrace();
}finally {
DbHelp.close(con, ps, rs);
}
return ar;
}
}
|