增删改查--项目基于JavaWeb+Servlet+MySQL的【学生管理系统】

目录

需求:

MySQL数据库:

​编辑

底层代码

工具类

DBHelper:

 实体类

班级实体类

爱好实体类

教员实体类

学生实体类

数据库访问层

班级数据库访问层接口:

班级数据库访问层:

 爱好数据库访问层接口:

 爱好数据库访问层:

教员数据库访问层接口:

教员数据库访问层:

学生数据库访问接口:

学生数据库访问:

业务逻辑层

班级业务逻辑层接口:

班级业务逻辑层:

爱好业务逻辑层接口:

爱好业务逻辑层:

教员业务逻辑层接口:

教员业务逻辑层:

学生业务逻辑层接口:

学生业务逻辑层:

页面效果

主界面

主界面代码:

 Servlet

删除

效果图:

删除后:

 删除的Servlet:

 增加

界面展示图:

 增加的Servlet:

增加班级和教员绑值功能的Servlet:

增加界面代码:

查看

查看界面图:

 查看Servlet:

查看界面代码:

修改

修改界面图:

修改前绑值Servlet:

修改后Servlet:

修改界面代码:


需求:

MySQL数据库:

上图所示我们要建四个表:学生、教师、班级以及爱好表

底层代码

工具类

config.properties:

#oracle9i
#driver=oracle.jdbc.driver.OracleDriver
#url=jdbc:oracle:thin:@localhost:1521:orcl
#user=scott
#pwd=123


#sql2005
#driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
#url=jdbc:sqlserver://localhost:1433;DatabaseName=test1
#user=sa
#pwd=123


#sql2000
#driver=com.microsoft.jdbc.sqlserver.SQLServerDriver
#url=jdbc:microsoft:sqlserver://localhost:1433;databaseName=unit6DB
#user=sa
#pwd=888888


#mysql
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db_20220613?useUnicode=true&characterEncoding=UTF-8&useSSL=false
user=root
pwd=123



DBHelper:

package com.oyang.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * 提供了一组获得或关闭数据库对象的方法
 * 
 */
public class DBHelper {
	private static String driver;
	private static String url;
	private static String user;
	private static String password;

	static {// 静态块执行一次,加载 驱动一次
		try {
			InputStream is = DBHelper.class
					.getResourceAsStream("config.properties");

			Properties properties = new Properties();
			properties.load(is);

			driver = properties.getProperty("driver");
			url = properties.getProperty("url");
			user = properties.getProperty("user");
			password = properties.getProperty("pwd");

			Class.forName(driver);
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	/**
	 * 获得数据连接对象
	 * 
	 * @return
	 */
	public static Connection getConnection() {
		try {
			Connection conn = DriverManager.getConnection(url, user, password);
			return conn;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public static void close(ResultSet rs) {
		if (null != rs) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
	}

	public static void close(Statement stmt) {
		if (null != stmt) {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
	}

	public static void close(Connection conn) {
		if (null != conn) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new RuntimeException(e);
			}
		}
	}

	public static void close(Connection conn, Statement stmt, ResultSet rs) {
		close(rs);
		close(stmt);
		close(conn);
	}

	public static boolean isOracle() {
		return "oracle.jdbc.driver.OracleDriver".equals(driver);
	}

	public static boolean isSQLServer() {
		return "com.microsoft.sqlserver.jdbc.SQLServerDriver".equals(driver);
	}
	
	public static boolean isMysql() {
		return "com.mysql.jdbc.Driver".equals(driver);
	}

	public static void main(String[] args) {
		Connection conn = DBHelper.getConnection();
		DBHelper.close(conn);
		System.out.println("isOracle:" + isOracle());
		System.out.println("isSQLServer:" + isSQLServer());
		System.out.println("isMysql:" + isMysql());
		System.out.println("数据库连接(关闭)成功");
	}
}

 实体类

班级实体类

package com.oyang.entity;

import java.io.Serializable;

/**
 * 实体类:班级类
 * @author yang
 *
 */
public class Class  implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2977202769889689270L;
	private int cid;//班级对象
	private String cname;//班级名称
	
	
	public int getCid() {
		return cid;
	}
	public void setCid(int cid) {
		this.cid = cid;
	}
	public String getCname() {
		return cname;
	}
	public void setCname(String cname) {
		this.cname = cname;
	}
	
	public Class() {
		// TODO Auto-generated constructor stub
	}
	public Class(int cid, String cname) {
		this.cid = cid;
		this.cname = cname;
	}
	@Override
	public String toString() {
		return "Class [cid=" + cid + ", cname=" + cname + "]";
	}
	
	
	
}

爱好实体类

package com.oyang.entity;

import java.io.Serializable;

/**
 * 实体类:爱好类
 * @author yang
 *
 */
public class Hobby  implements Serializable{


	private static final long serialVersionUID = 2260349911609805915L;
	private int hid;//爱好编号
	private String hname;//爱好名称
	public int getHid() {
		return hid;
	}
	public void setHid(int hid) {
		this.hid = hid;
	}
	public String getHname() {
		return hname;
	}
	public void setHname(String hname) {
		this.hname = hname;
	}

	public Hobby() {
		// TODO Auto-generated constructor stub
	}
	public Hobby(int hid, String hname) {
		this.hid = hid;
		this.hname = hname;
	}
	@Override
	public String toString() {
		return "Hobby [hid=" + hid + ", hname=" + hname + "]";
	}
	
	
	
	
}

教员实体类

package com.oyang.entity;

import java.io.Serializable;

/**
 * 实体类:教员类
 * @author yang
 *
 */
public class Theaher implements Serializable{
	

	private static final long serialVersionUID = 1L;
	private int tid;//教员编号
	private String tname;//教员名称
	private Class cid;//班级对象
	
	public int getTid() {
		return tid;
	}
	public void setTid(int tid) {
		this.tid = tid;
	}
	public String getTname() {
		return tname;
	}
	public void setTname(String tname) {
		this.tname = tname;
	}
	public Class getCid() {
		return cid;
	}
	public void setCid(Class cid) {
		this.cid = cid;
	}
	public Theaher() {
		// TODO Auto-generated constructor stub
	}
	public Theaher(int tid, String tname, Class cid) {
		this.tid = tid;
		this.tname = tname;
		this.cid = cid;
	}
	
	@Override
	public String toString() {
		return "Theaher [tid=" + tid + ", tname=" + tname + ", cid=" + cid + "]";
	}
	
}

学生实体类

package com.oyang.entity;

import java.io.Serializable;
import java.util.List;

/**
 * 实体类:学生类
 * @author yang
 *
 */
public class Student implements Serializable{
	
	private static final long serialVersionUID = 1L;
	
	private int sid;//学生编号
	private String sname;//学生名称
	private Class cl;//班级对象
	private Theaher th;//教员对象
	private List<Hobby> hy;//爱好集合

	public int getSid() {
		return sid;
	}
	public void setSid(int sid) {
		this.sid = sid;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public Class getCl() {
		return cl;
	}
	public void setCl(Class cl) {
		this.cl = cl;
	}
	public Theaher getTh() {
		return th;
	}
	public void setTh(Theaher th) {
		this.th = th;
	}
	public List<Hobby> getHy() {
		return hy;
	}
	public void setHy(List<Hobby> hy) {
		this.hy = hy;
	}
	public Student() {
		// TODO Auto-generated constructor stub
	}
	public Student(int sid, String sname, Class cl, Theaher th, List<Hobby> hy) {
		this.sid = sid;
		this.sname = sname;
		this.cl = cl;
		this.th = th;
		this.hy = hy;
	}
	public Student(String sname, Class cl, Theaher th, List<Hobby> hy) {
		this.sname = sname;
		this.cl = cl;
		this.th = th;
		this.hy = hy;
	}
	@Override
	public String toString() {
		return "Student [sid=" + sid + ", sname=" + sname + ", cl=" + cl + ", th=" + th + ", hy=" + hy + "]";
	}
	
}

数据库访问层

班级数据库访问层接口:

package com.oyang.dao;

import java.util.List;

import com.oyang.entity.Class;


/**
 * 班级数据库访问层接口
 * @author yang
 *
 */
public interface IClDao {

	/**
	 * 查询全部
	 * @return 班级集合
	 */
	public List<Class> getAll();
	
	/**
	 * 查询单个
	 * @param cid 班级编号
	 * @return 班级对象
	 */
	public Class getdg(int cid);

}

班级数据库访问层:

package com.oyang.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.oyang.entity.Class;
import com.oyang.util.DBHelper;

/**
 * 班级数据库访问层
 * @author yang
 *
 */
public class ClDao implements IClDao{

	//扩大作用域
	Connection conn = null;
	Statement stmt = null;
	ResultSet rs = null;

	
	@Override
	public List<Class> getAll() {
		List<Class> ls = new ArrayList<Class>();
		try {
			//连接数据库
			conn = DBHelper.getConnection();
			//定义sql语句
			String sql = "select * from tb_bj";
			//执行sql语句
			stmt = conn.prepareStatement(sql);
			//获得结果集
			rs = stmt.executeQuery(sql);
			//遍历结果集
			while(rs.next()) {
				//实例化
				Class cl = new Class(rs.getInt(1),rs.getString(2));
				//加到集合中
				ls.add(cl);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(conn, stmt, rs);
		}
		return ls;

	}


	@Override
	public Class getdg(int cid) {
		Class cl = new Class();
		try {
			//连接数据库
			conn = DBHelper.getConnection();
			//定义sql语句
			String sql = "select * from tb_bj where bid="+cid+"";
			//执行sql语句
			stmt = conn.prepareStatement(sql);
			//获得结果集
			rs = stmt.executeQuery(sql);
			//遍历结果集
			while(rs.next()) {
				//实例化
				cl = new Class(rs.getInt(1),rs.getString(2));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(conn, stmt, rs);
		}
		return cl;

	}
	
	public static void main(String[] args) {
		System.out.println(new ClDao().getdg(1));
	}

}

 爱好数据库访问层接口:

package com.oyang.dao;

import java.util.List;

import com.oyang.entity.Hobby;

/**
 * 爱好数据库访问层接口
 * @author yang
 *
 */
public interface IHyDao {

	/**
	 * 爱好集合
	 * @return 爱好集合
	 */
	public List<Hobby> getAll();
	
	/**
	 * 爱好查询单个
	 * @param hid 爱好编号
	 * @return 爱好对象
	 */
	public Hobby getdg(int aid);
}

 爱好数据库访问层:

package com.oyang.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.oyang.entity.Hobby;
import com.oyang.util.DBHelper;

/**
 * 爱好数据库访问层
 * @author yang
 *
 */
public class HyDao implements IHyDao{

	//扩大作用域
	Connection conn = null;
	Statement stmt = null;
	ResultSet rs = null;

	
	@Override
	public List<Hobby> getAll() {
		List<Hobby> ls = new ArrayList<Hobby>();
		try {
			//连接数据库
			conn = DBHelper.getConnection();
			//定义sql语句
			String sql = "select * from tb_ah";
			//执行sql语句
			stmt = conn.prepareStatement(sql);
			//获得结果集
			rs = stmt.executeQuery(sql);
			//遍历结果集
			while(rs.next()) {
				//实例化
				Hobby hy = new Hobby(rs.getInt(1),rs.getString(2));
				//加到集合中
				ls.add(hy);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(conn, stmt, rs);
		}
		return ls;
	}


	@Override
	public Hobby getdg(int aid) {
		Hobby  hy = new Hobby();
		try {
			//连接数据库
			conn = DBHelper.getConnection();
			//定义sql语句
			String sql = "select * from tb_ah where aid="+aid+"";
			//执行sql语句
			stmt = conn.prepareStatement(sql);
			//获得结果集
			rs = stmt.executeQuery(sql);
			//遍历结果集
			while(rs.next()) {
				//实例化
				hy = new Hobby(rs.getInt(1),rs.getString(2));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(conn, stmt, rs);
		}
		return hy;
	}

	
}

教员数据库访问层接口:

package com.oyang.dao;

import java.util.List;

import com.oyang.entity.Theaher;

/**
 * 教员数据库访问层接口
 * @author yang
 *
 */
public interface IThDao {
	/**
	 * 查询全部
	 * @return 教员集合
	 */
	public List<Theaher> getAll();

	
	/**
	 * 查询单个
	 * @param jid 教员编号
	 * @return 教员对象
	 */
	public Theaher getdg(int jid);
	
	
}

教员数据库访问层:

package com.oyang.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.oyang.entity.Class;
import com.oyang.entity.Theaher;
import com.oyang.util.DBHelper;

/**
 * 教员数据库访问层接口
 * @author yang
 *
 */
public class ThDao implements IThDao{

	//扩大作用域
	Connection conn = null;
	Statement stmt = null;
	ResultSet rs = null;

	
	@Override
	public List<Theaher> getAll() {
		List<Theaher> ls = new ArrayList<Theaher>();
		try {
			//连接数据库
			conn = DBHelper.getConnection();
			//定义sql语句
			String sql = "select * from tb_jiaoy";
			//执行sql语句
			stmt = conn.prepareStatement(sql);
			//获得结果集
			rs = stmt.executeQuery(sql);
			//遍历结果集
			while(rs.next()) {
				//实例化班级
				Class cl = new Class(rs.getInt(1),rs.getString(2));
				//实例化
				Theaher th = new Theaher(rs.getInt(1),rs.getString(2),cl);
				//加到集合中
				ls.add(th);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(conn, stmt, rs);
		}
		return ls;

	}


	@Override
	public Theaher getdg(int tid) {
		//教师
		Theaher th = new Theaher();
		try {
			//连接数据库
			conn = DBHelper.getConnection();
			//定义sql语句
			String sql = "select * from (\r\n" + 
					"select t.*,c.bname from tb_jiaoy t,tb_bj c where t.bid = c.bid\r\n" + 
					")a  where a.jid = "+tid+"";
			//执行sql语句
			stmt = conn.prepareStatement(sql);
			//获得结果集
			rs = stmt.executeQuery(sql);
			//遍历结果集
			while(rs.next()) {
				//实例化班级
				Class cl = new Class(rs.getInt("bid"),rs.getString("bname"));
				//实例化
				th = new Theaher(rs.getInt("jid"),rs.getString("jname"),cl);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(conn, stmt, rs);
		}
		return th;

	}
	
	public static void main(String[] args) {
		System.out.println(new ThDao().getdg(5));
	}


}

学生数据库访问接口:

package com.oyang.dao;

import java.util.List;

import com.oyang.entity.Class;
import com.oyang.entity.Hobby;
import com.oyang.entity.Student;
import com.oyang.entity.Theaher;

/**
 * 学生数据库访问接口
 * @author yang
 *
 */
public interface IStuDao {
	
	/**
	 * 查询全部
	 * @param cid 班级编号
	 * @param tid 教员id
	 * @param hid 爱好id
	 * @param pageIndex 第几页
	 * @param pageSize  总页数
	 * @return 学生集合
	 */
	public List<Student> getAll(String xid,String jid,String aid,int pageIndex,int pageSize);
	
	/**
	 * 查询单个学生
	 * @param aid 学生编号
	 * @return 返回单个学生
	 */
	public Student getdg(int aid);
	
	
	
	/**
	 * 增加学生
	 * @param xname 学生姓名
	 * @param cl 班级对象
	 * @param th 教员对象 
	 * @param hy 爱好集合
	 * @return 影响行数
	 */
	public int addStu(String xname, Class cl, Theaher th, List<Hobby> hy);
	
	
	/**
	 * 删除学生
	 * @param xid 学生编号
	 * @return  影响行数
	 */
	public int delStu(int xid);
	
	
	/**
	 * 修改学生信息
	 * @param xid 学生id
	 * @param xname 学生姓名
	 * @param cl 班级对象
	 * @param th 教师对象
	 * @param hy 爱好集合
	 * @return 影响行数
	 */
	public int updStu(int xid,String xname, Class cl, Theaher th, List<Hobby> hy);
	
	
	
	/**
	 * 总行数
	 * @param bid 班级编号
	 * @param jid 教员id
	 * @param aid 爱好id
	 * @return
	 */
	public int count(String bid,String jid,String aid);
	
	
}

学生数据库访问:

package com.oyang.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.sql.Statement;

import com.oyang.biz.HyBiz;
import com.oyang.biz.IHyBiz;
import com.oyang.entity.Class;
import com.oyang.entity.Hobby;
import com.oyang.entity.Student;
import com.oyang.entity.Theaher;
import com.oyang.util.DBHelper;

/**
 * 学生数据库访问接口
 * 
 * @author yang
 *
 */
public class StuDao implements IStuDao{
	
	//扩大作用域
	Connection conn = null;
	Statement stmt = null;
	ResultSet rs = null;
	PreparedStatement ps = null;
	
	//爱好
	IHyBiz ihb = new HyBiz();
	
	@Override
	public List<Student> getAll(String bid,String jid,String hid,int pageIndex,int pageSize) {
		int a = (pageIndex-1)*pageSize;
		List<Student> ls = new ArrayList<Student>();
		try {
			//连接数据库
			conn = DBHelper.getConnection();
			//定义sql语句
			String sql = "select * from (\r\n" + 
					"select * from (\r\n" + 
					"select * from (\r\n" + 
					"select *  from (\r\n" + 
					"select a.*,b.aname,c.bname,d.jname from tb_stu a,tb_ah b,tb_bj c,tb_jiaoy d where a.bid=c.bid and a.jid=d.jid and a.aid = b.aid\r\n" + 
					")q  "+bid+"\r\n" + 
					")w "+jid+"\r\n" + 
					")e "+hid+"\r\n" + 
					")r limit "+a+","+pageSize+"";
			//执行sql语句
			stmt = conn.prepareStatement(sql);
			//获得结果集
			rs = stmt.executeQuery(sql);
			//遍历结果及
			while(rs.next()) {
				List<Hobby> hy = new ArrayList<>();
				String str = rs.getString("aid");
				String[] split = str.split(",");
				for (String hids : split) {
					//查询单个
					Hobby hby = ihb.getdg(Integer.parseInt(hids));
					hy.add(hby);
				}
				
				//班级
				Class cl = new Class(rs.getInt("bid"),rs.getString("bname"));
				//教员
				Theaher th = new Theaher(rs.getInt("jid"),rs.getString("jname"),cl);
				//学生
				Student stu = new Student(rs.getInt("xid"),rs.getString("xname"),cl,th,hy);
				//加到集合中
				ls.add(stu);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(conn, stmt, rs);
		}
		return ls;
	}
	
	@Override
	public Student getdg(int aid) {
		Student stu = new Student();
		try {
			//连接数据库
			conn = DBHelper.getConnection();
			//定义sql语句
			String sql = "select * from (\r\n" + 
					"select a.*,b.aname,c.bname,d.jname from tb_stu a,tb_ah b,tb_bj c,tb_jiaoy d where a.bid=c.bid and a.jid=d.jid and a.aid = b.aid ) e where e.xid="+aid+"";
			//执行sql语句
			stmt = conn.prepareStatement(sql);
			//获得结果集
			rs = stmt.executeQuery(sql);
			//遍历结果及
			while(rs.next()) {
				List<Hobby> hy = new ArrayList<>();
				String str = rs.getString("aid");
				String[] split = str.split(",");
				for (String hid : split) {
					//查询单个
					Hobby hby = ihb.getdg(Integer.parseInt(hid));
					hy.add(hby);
				}
				//班级
				Class cl = new Class(rs.getInt("bid"),rs.getString("bname"));
				//教员
				Theaher th = new Theaher(rs.getInt("jid"),rs.getString("jname"),cl);
				//学生
				stu = new Student(rs.getInt("xid"),rs.getString("xname"),cl,th,hy);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(conn, stmt, rs);
		}
		return stu;
	}

	@Override
	public int addStu(String sname, Class cl, Theaher th, List<Hobby> hy) {
		String hh = "";
		for (int i = 0; i < hy.size(); i++) {
			hh+=hy.get(i).getHid()+",";
		}
		int n = 0;
		try {
			//连接数据库
			conn = DBHelper.getConnection();
			//定义sql语句
			String sql = "insert into tb_stu(xname,bid,jid,aid) values('"+sname+"',"+cl.getCid()+","+th.getTid()+",'"+hh+"')";
			//执行sql语句
			stmt = conn.prepareStatement(sql);
			//返回执行的行数
			n = stmt.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(conn, ps, rs);
		}
		return n;
	}



	@Override
	public int delStu(int aid) {
		int n = 0;
		try {
			//连接数据库
			conn = DBHelper.getConnection();
			//定义sql语句
			String sql = "delete from tb_stu where xid="+aid+"";
			//执行sql语句
			stmt = conn.prepareStatement(sql);
			//返回执行的行数
			n = stmt.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(conn, ps, rs);
		}
		return n;
	}



	@Override
	public int updStu(int aid, String sname, Class cl, Theaher th, List<Hobby> hy) {
		int n = 0;
		String hh = "";
		for (int i = 0; i < hy.size(); i++) {
			hh+=hy.get(i).getHid()+",";
		}
		try {
			//连接数据库
			conn = DBHelper.getConnection();
			//定义sql语句
			String sql = "update tb_stu set xname='"+sname+"',bid="+cl.getCid()+",jid="+th.getTid()+",aid='"+hh+"' where xid="+aid+"";
			//执行sql语句
			stmt = conn.prepareStatement(sql);
			//返回执行的行数
			n = stmt.executeUpdate(sql);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(conn, ps, rs);
		}
		return n;

	}



	@Override
	public int count(String cid,String tid,String hid) {
		int n = 0;
		try {
			//连接数据库
			conn = DBHelper.getConnection();
			//定义sql语句
			String sql = "select count(*) from (\r\n" + 
					"select * from (\r\n" + 
					"select * from (\r\n" + 
					"select *  from (\r\n" + 
					"select a.*,b.aname,c.bname,d.jname from tb_stu a,tb_ah b,tb_bj c,tb_jiaoy d where a.bid=c.bid and a.jid=d.jid and a.aid = b.aid\r\n" + 
					")q  "+cid+" \r\n" + 
					")w  "+tid+" \r\n" + 
					")e  "+hid+" \r\n" + 
					")t";
//			System.out.println(sql);
			//执行sql语句
			stmt = conn.prepareStatement(sql);
			//结果集
			rs = stmt.executeQuery(sql);
			if(rs.next()) {
				//返回执行的行数
				n = rs.getInt(1);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBHelper.close(conn, ps, rs);
		}
		return n;
	}

}

业务逻辑层

班级业务逻辑层接口:

package com.oyang.biz;

import java.util.List;

import com.oyang.entity.Class;

/**
 * 班级业务逻辑层接口
 * @author yang
 *
 */
public interface IClBiz {
	/**
	 * 查询全部
	 * @return 班级集合
	 */
	public List<Class> getAll();
	
	/**
	 * 查询单个
	 * @param cid 班级编号
	 * @return 班级对象
	 */
	public Class getdg(int cid);


}

班级业务逻辑层:

package com.oyang.biz;

import java.util.List;

import com.oyang.dao.ClDao;
import com.oyang.dao.IClDao;
import com.oyang.entity.Class;

/**
 * 班级业务逻辑层
 * @author yang
 *
 */
public class ClBiz implements IClBiz{

	//调用数据库访问层
	IClDao icd = new ClDao();
	
	@Override
	public List<Class> getAll() {
		return icd.getAll();
	}

	@Override
	public Class getdg(int cid) {
		return icd.getdg(cid);
	}

	
	
}

爱好业务逻辑层接口:

package com.oyang.biz;

import java.util.List;

import com.oyang.entity.Hobby;

/**
 * 爱好业务逻辑层接口
 * @author yang
 *
 */
public interface IHyBiz {

	/**
	 * 爱好集合
	 * @return 爱好集合
	 */
	public List<Hobby> getAll();
	
	/**
	 * 爱好查询单个
	 * @param hid 爱好编号
	 * @return 爱好对象
	 */
	public Hobby getdg(int aid);
}

爱好业务逻辑层:

package com.oyang.biz;

import java.util.List;

import com.oyang.dao.HyDao;
import com.oyang.dao.IHyDao;
import com.oyang.entity.Hobby;

/**
 * 爱好业务逻辑层
 * @author yang
 *
 */
public class HyBiz implements IHyBiz{

	//调用数据库访问层
	IHyDao ihd = new HyDao();
	
	@Override
	public List<Hobby> getAll() {
		return ihd.getAll();
	}

	@Override
	public Hobby getdg(int hid) {
		return ihd.getdg(hid);
	}

}

教员业务逻辑层接口:

package com.oyang.biz;

import java.util.List;

import com.oyang.entity.Theaher;

/**
 * 教员业务逻辑层接口
 * @author yang
 *
 */
public interface IThBiz {

	/**
	 * 查询全部
	 * @return 教员集合
	 */
	public List<Theaher> getAll();

	
	/**
	 * 查询单个
	 * @param jid 教员编号
	 * @return 教员对象
	 */
	public Theaher getdg(int jid);
	

	
}

教员业务逻辑层:

package com.oyang.biz;

import java.util.List;

import com.oyang.dao.IThDao;
import com.oyang.dao.ThDao;
import com.oyang.entity.Theaher;

/**
 * 教员业务逻辑层
 * @author Administrator
 *
 */
public class ThBiz implements IThBiz{

	//调用数据库访问层
	IThDao itd = new ThDao();
	
	@Override
	public List<Theaher> getAll() {
		return itd.getAll();
	}

	@Override
	public Theaher getdg(int tid) {
		return itd.getdg(tid);
	}

	
	
	
}

学生业务逻辑层接口:

package com.oyang.biz;

import java.util.List;

import com.oyang.entity.Class;
import com.oyang.entity.Hobby;
import com.oyang.entity.Student;
import com.oyang.entity.Theaher;

/**
 * 学生业务逻辑层接口
 * @author yang
 *
 */
public interface IStuBiz {

	/**
	 * 查询全部
	 * @param cid 班级编号
	 * @param tid 教员id
	 * @param hid 爱好id
	 * @param pageIndex 第几页
	 * @param pageSize  总页数
	 * @return 学生集合
	 */
	public List<Student> getAll(String xid,String jid,String aid,int pageIndex,int pageSize);
	
	/**
	 * 查询单个学生
	 * @param aid 学生编号
	 * @return 返回单个学生
	 */
	public Student getdg(int aid);
	
	
	
	/**
	 * 增加学生
	 * @param xname 学生姓名
	 * @param cl 班级对象
	 * @param th 教员对象 
	 * @param hy 爱好集合
	 * @return 影响行数
	 */
	public int addStu(String xname, Class cl, Theaher th, List<Hobby> hy);
	
	
	/**
	 * 删除学生
	 * @param xid 学生编号
	 * @return  影响行数
	 */
	public int delStu(int xid);
	
	
	/**
	 * 修改学生信息
	 * @param xid 学生id
	 * @param xname 学生姓名
	 * @param cl 班级对象
	 * @param th 教师对象
	 * @param hy 爱好集合
	 * @return 影响行数
	 */
	public int updStu(int xid,String xname, Class cl, Theaher th, List<Hobby> hy);
	
	
	
	/**
	 * 总行数
	 * @param bid 班级编号
	 * @param jid 教员id
	 * @param aid 爱好id
	 * @return
	 */
	public int count(String bid,String jid,String aid);

	
}

学生业务逻辑层:

package com.oyang.biz;

import java.util.List;

import com.oyang.dao.IStuDao;
import com.oyang.dao.StuDao;
import com.oyang.entity.Class;
import com.oyang.entity.Hobby;
import com.oyang.entity.Student;
import com.oyang.entity.Theaher;

/**
 * 学生业务逻辑层 
 * 
 * @author yang 
 *
 */
public class StuBiz implements IStuBiz {

	//调用数据库访问层
	IStuDao isd = new StuDao();
	
	@Override
	public List<Student> getAll(String cid,String tid,String hid,int pageIndex,int pageSize) {
		return isd.getAll(cid, tid, hid, pageIndex, pageSize);
	}

	@Override
	public Student getdg(int sid) {
		return isd.getdg(sid);
	}

	@Override
	public int addStu(String sname, Class cl, Theaher th, List<Hobby> hy) {
		return isd.addStu(sname, cl, th, hy);
	}

	@Override
	public int delStu(int sid) {
		return isd.delStu(sid);
	}

	@Override
	public int updStu(int sid, String sname, Class cl, Theaher th, List<Hobby> hy) {
		return isd.updStu(sid, sname, cl, th, hy);
	}

	@Override
	public int count(String cid,String tid,String hid) {
		return isd.count(cid, tid, hid);
	}
	
}

页面效果

主界面

效果图:

主界面代码:

<%@ 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 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>

	<c:if test="${empty stu }">
	 			<jsp:forward page="IndexServlet"></jsp:forward>
	 </c:if>
	<center>
		<h2>主界面</h2>
		<form action="IndexServlet" method="post">
		<table border="1px">
			<tr>
				<td colspan="6px">
					教员:
					<select name="jid">
						<option value=""  >请选择</option>
					<c:forEach items="${theaher12 }" var="t">
						<option value=" where jid='${t.tid }'"  >${t.tname}</option>
					</c:forEach>
					</select>
					班级:
					<select name="bid">
						<option value=""  >请选择</option>
					<c:forEach items="${clas12 }" var="c">
						<option value=" where bid='${c.cid }'"  >${c.cname}</option>
					</c:forEach>
					</select>
					爱好:
					<input type="checkbox" name="hid" value="1,"  >篮球
					<input type="checkbox" name="hid" value="2,"  >足球
					<input type="checkbox" name="hid" value="3,"  >唱歌
					<input type="checkbox" name="hid" value="4,"  >跳舞
					<input type="submit" value="查询">
					<input type="button" value="增加" onclick="add()">
				</td>
			</tr>
			<tr>
				<td>学生编号</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.th.tname}</td>
				<td>${s.cl.cname}</td>
				<td>
					<c:forEach items="${s.hy}" var="x">
						${x.hname}
					</c:forEach>
				</td> 
				<td>
					<a href="CheckServlet?sid=${s.sid }">查看</a>
					<a href="XgServlet?sid=${s.sid }">修改</a>
					<a  onclick="return confirm('你确定要删除吗?')"  href="DeleteServlet?sid=${s.sid }">删除</a>
				</td>
			</tr>
			</c:forEach>
		</table>
		 <div>
		 	第${pageIndex}页,&nbsp;&nbsp;共${pagecount}页,&nbsp;&nbsp;总记录${count}条
		 	<a href="IndexServlet?pid=1&hid=${a}&jid=${b}&bid=${c}">首页</a>
		 	<a href="IndexServlet?pid=${pageIndex>1?pageIndex-1:1}&hid=${a}&jid=${b}&bid=${c}">上一页</a>
		 	<input type="text" style=" width:15px" width="5px" name="gid" id="gid">
		 			<input type="button" style=" width:30px;" width="5px" value="go" onclick="go('gid',${a})">
		 	<!-- <a>go<input type="text" onblur="go('gid')" style=" width:15px" width="5px" name="gid" id="gid"></a> -->
		 	<a href="IndexServlet?pid=${pageIndex<pagecount?pageIndex+1:pagecount}&hid=${a}&jid=${b}&bid=${c}">下一页</a>
		 	<a href="IndexServlet?pid=${pagecount}&hid=${a}&jid=${b}&bid=${c}">尾页</a>
		 </div>
		</form>
	</center>
	<script type="text/javascript">
	function $(rid){
		return document.getElementById(rid);
	}
	function add() {
		location.href="FoundServlet";
	}
	
	function go(id,a,b,c){
		var sd=a;
		var sb=b;
		var sc=c;
		if(sd==null){
			sd="";
		}
		if(sb==null){
			sb="";
		}
		if(sc==null){
			sc="";
		}
		var aa = document.getElementById("gid");
		var bb = aa.value;
		if(isNaN(bb)||bb<0){
			alert("请输入正确的数字");
		}
		else{
			location.href="IndexServlet?gid="+bb+"&hid="+sd+"&jid="+sb+"&bid="+sc+"";
		}
	}
</script>
</body>
</html>

 Servlet

package com.oyang.servlet;

import java.io.IOException;
import java.util.List;

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 com.oyang.biz.ClBiz;
import com.oyang.biz.HyBiz;
import com.oyang.biz.IClBiz;
import com.oyang.biz.IHyBiz;
import com.oyang.biz.IStuBiz;
import com.oyang.biz.IThBiz;
import com.oyang.biz.StuBiz;
import com.oyang.biz.ThBiz;
import com.oyang.entity.Class;
import com.oyang.entity.Hobby;
import com.oyang.entity.Student;
import com.oyang.entity.Theaher;

/**
 * 查看全部&模糊查询&分页功能
 * @author yang 
 */
@WebServlet("/IndexServlet")
public class IndexServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置编码方式
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		
		//扩大作用域
		int pageIndex = 1;
		int pageSize = 2;
		//接收表单 
		String cid = request.getParameter("bid");
		if(cid==null) {
			cid="";
		}
		
		String tid= request.getParameter("jid");
		if(tid==null) {
			tid="";
		}
		String[] hids = request.getParameterValues("hid");
		
		String hid = "";
		String hidd ="";//用来拼接
		if(hids==null) {
			hidd="";
			hid="";
			System.out.println("a+++++");
			System.out.println(tid+"==========");
			System.out.println(cid);
		}
		else {
			System.out.println("不为空");
			for (String str : hids) {
				String asd=str;
				System.out.println(str);
				if(asd.contains(",")) {
					System.out.println("按市场1");
					String[] split = asd.split(",");
					for (String string : split) {
						System.out.println(string);
						hidd+=string+",";//用空格进行隔开
						hid= " where aid like '%"+hidd+"%' ";
					}
				}
				else {
					if(str==null) {
						System.out.println("按市场2");
						hidd+=str+",";//用空格进行隔开
						hid= " where aid like '%"+hidd+"%' ";
					}
				}
				/*if(str.contains(",")){
					System.out.println("按市场");
					hidd+=str+",";//用逗号进行隔开
					hid= " where aid like '%"+hidd+"%' ";
				}*/
			}
		}
		
		String pid = request.getParameter("pid");
		if(pid!=null){//说明点了x页
			pageIndex = Integer.parseInt(pid);
		}
		
		String gid = request.getParameter("gid");
		if(gid==null) {
			gid="";
		}
		else if(gid=="") {
			gid="";
		}
		else{
			pageIndex = Integer.parseInt(gid);
		}
		
		
		//业务逻辑层
		IStuBiz isb = new StuBiz();
		IHyBiz ihb = new HyBiz();//爱好
		IThBiz itb = new ThBiz();//教员
		IClBiz icb = new ClBiz();//班级

		//调用查询全部的方法
		List<Student> stu = isb.getAll(cid, tid,hid, pageIndex, pageSize);
		List<Hobby> hobby12 = ihb.getAll();
		List<Theaher> theaher12 = itb.getAll();
		List<Class> clas12 = icb.getAll();
		int count = isb.count(cid, tid, hid);
		
		int pagecount=0;
		if(count%pageSize==1) {
			pagecount=count/pageSize+1;
		}
		else {
			pagecount=count/pageSize;
		}
		System.out.println(stu);
		if(stu.size()!=0) {
			//加到集合中
			request.setAttribute("stu", stu);
			request.setAttribute("hobby12", hobby12);
			request.setAttribute("theaher12", theaher12);
			request.setAttribute("clas12", clas12);
			request.setAttribute("count", count);
			request.setAttribute("pagecount", pagecount);
			request.setAttribute("pageIndex", pageIndex);
			request.setAttribute("a", hidd);
			request.setAttribute("b", tid);
			request.setAttribute("c", cid);
			//转发
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}
		else {
			System.out.println("集合为空");
		}
		
	}

}

删除

效果图:

删除后:

 删除的Servlet:

package com.oyang.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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 com.oyang.biz.IStuBiz;
import com.oyang.biz.StuBiz;

/**
 * 删除功能
 * @author yang 
 *
 */
@WebServlet("/DeleteServlet")
public class DeleteServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置编码方式
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		//out
		PrintWriter out = response.getWriter();
		
		//获取表单提交过来的值
		String sid = request.getParameter("sid");
		//业务逻辑
		IStuBiz isb = new StuBiz();
		//删除
		int n = isb.delStu(Integer.parseInt(sid));
		//判断
		if(n>0) {
			out.print("<script>alert('删除成功');location.href='index.jsp'</script>");
		}
		else {
			out.print("<script>alert('删除失败');location.href='index.jsp'</script>");
		}
	}

}

 增加

界面展示图:

 增加的Servlet:

package com.oyang.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

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 com.oyang.biz.ClBiz;
import com.oyang.biz.HyBiz;
import com.oyang.biz.IClBiz;
import com.oyang.biz.IHyBiz;
import com.oyang.biz.IStuBiz;
import com.oyang.biz.IThBiz;
import com.oyang.biz.StuBiz;
import com.oyang.biz.ThBiz;
import com.oyang.entity.Class;
import com.oyang.entity.Hobby;
import com.oyang.entity.Student;
import com.oyang.entity.Theaher;

/**
 * 增加功能
 * @author yang 
 *
 */
@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置编码方式
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");

		// 获取表单提交过来的值
		String sname = request.getParameter("sname");
		String tid = request.getParameter("tid");
		String cid = request.getParameter("cid");
		String[] hids = request.getParameterValues("hid");
		String hsy = "";// 用来拼接
		for (String str : hids) {
			hsy += str + " ";// 用空格进行隔开
		}

		PrintWriter out = response.getWriter();

		// 业务逻辑层
		IThBiz itb = new ThBiz();
		IClBiz icb = new ClBiz();
		IHyBiz ihb = new HyBiz();
		IStuBiz isb = new StuBiz();

		List<Hobby> hy = new ArrayList<>();
		String[] split = hsy.split(" ");
		for (String hid : split) {
			// 查询单个
			Hobby hby = ihb.getdg(Integer.parseInt(hid));
			hy.add(hby);
		}
		// 教员查询单个
		Theaher th = itb.getdg(Integer.parseInt(tid));
		// 班级查询单个
		Class cl = icb.getdg(Integer.parseInt(cid));
		// 调用增加的方法
		int n = isb.addStu(sname, cl, th, hy);
		if (n > 0) {
			out.print("<script>alert('增加成功');location.href='index.jsp'</script>");
		} else {
			out.print("<script>alert('增加失败');location.href='index.jsp'</script>");
		}

	}

}

增加班级和教员绑值功能的Servlet:

package com.oyang.servlet;

import java.io.IOException;
import java.util.List;

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 com.oyang.biz.ClBiz;
import com.oyang.biz.HyBiz;
import com.oyang.biz.IClBiz;
import com.oyang.biz.IHyBiz;
import com.oyang.biz.IThBiz;
import com.oyang.biz.ThBiz;
import com.oyang.entity.Class;
import com.oyang.entity.Hobby;
import com.oyang.entity.Theaher;

/**
 * 增加班级和教员绑值功能
 * @author yang 
 *
 */
@WebServlet("/FoundServlet")
public class FoundServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置编码方式
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");

		//业务逻辑层
		IHyBiz ihb = new HyBiz();//爱好
		IThBiz itb = new ThBiz();//教员
		IClBiz icb = new ClBiz();//班级
		
		//调用查询全部的方法
		List<Hobby> hobby = ihb.getAll();
		List<Theaher> theaher = itb.getAll();
		List<Class> clas = icb.getAll();
		
		//判断
		if(hobby!=null&&theaher!=null&&clas!=null) {
			//加到集合中
			request.setAttribute("hobby", hobby);
			request.setAttribute("theaher", theaher);
			request.setAttribute("clas", clas);
			//转发
			request.getRequestDispatcher("add.jsp").forward(request, response);
		}
		else {
			System.out.println("集合为空");
		}
		
	}

}

增加界面代码:

<%@ 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 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>
	<center>
	<h2>增加界面</h2>
	<form action="AddServlet" method="post">
		<table>
			<tr>
				<td>学生姓名</td>
				<td><input type="text" name="sname"></td>
			</tr>
			<tr>
				<td>学生教员</td>
				<td>
					<select name="tid">
					<c:forEach items="${theaher }" var="t">
						<option value="${t.tid }">${t.tname }</option>
					</c:forEach>
					</select>
				</td>
			</tr>
			<tr>
				<td>学生班级</td>
				<td>
					<select name="cid">
					<c:forEach items="${clas }" var="c">
						<option value="${c.cid }">${c.cname}</option>
					</c:forEach>
					</select>
				</td>
			</tr>
			<tr>
				<td>学生爱好</td>
				<td>
				<c:forEach items="${hobby }" var="h">
					<input type="checkbox" name="hid" value="${h.hid }">${h.hname }
				</c:forEach>	
				</td>
			</tr>
			
		</table>
		<input type="submit" value="确定">
		<input type="reset" value="清空">
		</form>
		<a href="index.jsp">返回</a>
	</center>
</body>
</html>

查看

查看界面图:

 查看Servlet:

package com.oyang.servlet;

import java.io.IOException;
import java.util.List;

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 com.oyang.biz.ClBiz;
import com.oyang.biz.HyBiz;
import com.oyang.biz.IClBiz;
import com.oyang.biz.IHyBiz;
import com.oyang.biz.IStuBiz;
import com.oyang.biz.IThBiz;
import com.oyang.biz.StuBiz;
import com.oyang.biz.ThBiz;
import com.oyang.entity.Class;
import com.oyang.entity.Hobby;
import com.oyang.entity.Student;
import com.oyang.entity.Theaher;

/**
 * 查看功能
 */
@WebServlet("/CheckServlet")
public class CheckServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置编码方式
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");

		// 接收表单提交过来的值
		String sid = request.getParameter("sid");

		// 业务逻辑层
		IHyBiz ihb = new HyBiz();// 爱好
		IThBiz itb = new ThBiz();// 教员
		IClBiz icb = new ClBiz();// 班级
		IStuBiz isb = new StuBiz();// 学生

		// 调用查询全部的方法
		List<Hobby> hobby2 = ihb.getAll();
		List<Theaher> theaher2 = itb.getAll();
		List<Class> clas2 = icb.getAll();
		Student student2 = isb.getdg(Integer.parseInt(sid));
		List<Hobby> hy = student2.getHy();
		String bb = "";
		for (Hobby hobby : hy) {
			int hid = hobby.getHid();
			bb += hid + " ";
		}
		// Student student2 = isb.getdg(2);
		// 判断
		if (hobby2 != null && theaher2 != null && clas2 != null && student2 != null) {
			// 加到集合中
			request.setAttribute("hobby2", hobby2);
			request.setAttribute("theaher2", theaher2);
			request.setAttribute("clas2", clas2);
			request.setAttribute("student2", student2);
			request.setAttribute("bb", bb);
			// 转发
			request.getRequestDispatcher("check.jsp").forward(request, response);
		} else {
			System.out.println("集合为空");
		}
	}

}

查看界面代码:

<%@ 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 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>
	<center>
	<h2>查看界面</h2>
	<form action="#" method="post">
		<table>
			<tr>
				<td>学生编号</td>
				<td><input type="text" name="sid" value="${student2.sid }" readonly="readonly"></td>
			</tr>
			<tr>
				<td>学生姓名</td>
				<td><input type="text" name="sname" value="${student2.sname }" readonly="readonly"></td>
			</tr>
			<tr>
				<td>学生教员</td>
				<td>
					<select name="tid">
					<c:forEach items="${theaher2 }" var="t">
						<option value="${t.tid }" <c:if test="${student2.th.tid==t.tid }">selected="selected"</c:if>  disabled="disabled">${t.tname }</option>
					</c:forEach>
					</select>
				</td>
			</tr>
			 <tr>
				<td>学生班级</td>
				<td>
					<select name="cid">
					<c:forEach items="${clas2 }" var="c">
						<option value="${c.cid }" <c:if test="${student2.cl.cid==c.cid }">selected="selected"</c:if>  disabled="disabled">${c.cname}</option>
					</c:forEach>
					</select>
				</td>
			</tr>
			<tr>
				<td>学生爱好</td>
				<td>
					<input type="checkbox" name="hid" value="1"  <c:if test="${bb.contains('1 ') }">checked</c:if>>篮球
					<input type="checkbox" name="hid" value="2"  <c:if test="${bb.contains('2 ') }">checked</c:if>>足球
					<input type="checkbox" name="hid" value="3"  <c:if test="${bb.contains('3 ') }">checked</c:if>>唱歌
					<input type="checkbox" name="hid" value="4"  <c:if test="${bb.contains('4 ') }">checked</c:if>>跳舞
				</td>
				<%-- <td>
				<c:forEach items="${hobby2 }" var="h">
					<input type="checkbox" name="hid" value="${h.hid }" <c:if test="${h.hid.contains('h.hid ')}">checked</c:if> disabled="disabled">${h.hname }
				</c:forEach>	
				</td> --%>
			</tr>
			
		</table>
		<a href="index.jsp">返回</a>
		</form>
		
	</center>
</body>
</html>

修改

修改界面图:

修改前绑值Servlet:

package com.oyang.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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 com.oyang.biz.ClBiz;
import com.oyang.biz.HyBiz;
import com.oyang.biz.IClBiz;
import com.oyang.biz.IHyBiz;
import com.oyang.biz.IStuBiz;
import com.oyang.biz.IThBiz;
import com.oyang.biz.StuBiz;
import com.oyang.biz.ThBiz;
import com.oyang.entity.Class;
import com.oyang.entity.Hobby;
import com.oyang.entity.Student;
import com.oyang.entity.Theaher;

/**
 * 修改前
 * @author yang 
 *
 */
@WebServlet("/XgServlet")
public class XgServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置编码方式
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		
		//接收表单提交过来的值
		String sid = request.getParameter("sid");

		//业务逻辑层
		IHyBiz ihb = new HyBiz();//爱好
		IThBiz itb = new ThBiz();//教员
		IClBiz icb = new ClBiz();//班级
		IStuBiz isb = new StuBiz();//学生
		
		//调用查询全部的方法
		List<Hobby> hobby1 = ihb.getAll();
		List<Theaher> theaher1 = itb.getAll();
		List<Class> clas1 = icb.getAll();
		Student student1 = isb.getdg(Integer.parseInt(sid));
//		Student student1 = isb.getdg(3);
		List<Hobby> hy = student1.getHy();
		String aa = "";
		for (Hobby hobby : hy) {
			int hid = hobby.getHid();
			aa+=hid+" ";
		}
//		System.out.println(aa);
//		Student student1 = isb.getdg(2);
		//判断
		if(hobby1!=null&&theaher1!=null&&clas1!=null&&student1!=null) {
			//加到集合中
			request.setAttribute("hobby1", hobby1);
			request.setAttribute("theaher1", theaher1);
			request.setAttribute("clas1", clas1);
			request.setAttribute("student1", student1);
			request.setAttribute("aa", aa);
			//转发
			request.getRequestDispatcher("update.jsp").forward(request, response);
		}
		else {
			System.out.println("集合为空");
		}
	}

}

修改后Servlet:

package com.oyang.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

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 com.oyang.biz.ClBiz;
import com.oyang.biz.HyBiz;
import com.oyang.biz.IClBiz;
import com.oyang.biz.IHyBiz;
import com.oyang.biz.IStuBiz;
import com.oyang.biz.IThBiz;
import com.oyang.biz.StuBiz;
import com.oyang.biz.ThBiz;
import com.oyang.entity.Class;
import com.oyang.entity.Hobby;
import com.oyang.entity.Theaher;

/**
 * 修改功能
 * @author yang 
 *
 */
@WebServlet("/UpdateServlet")
public class UpdateServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置编码方式
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");

		
		//获取表单提交过来的值
		String sid = request.getParameter("sid");
		String sname = request.getParameter("sname");
		String tid = request.getParameter("tid");
		String cid = request.getParameter("cid");
		String [] hids = request.getParameterValues("hid");
	    String hsy = "";//用来拼接
		for (String str : hids) {
			hsy+=str+" ";//用空格进行隔开
		}
		
		PrintWriter out = response.getWriter();
		
		//业务逻辑层
		IThBiz itb = new ThBiz();
		IClBiz icb = new ClBiz();
		IHyBiz ihb = new HyBiz();
		IStuBiz isb = new StuBiz();
		
		List<Hobby> hy = new ArrayList<>();
		String[] split = hsy.split(" ");
		for (String hid : split) {
			//查询单个
			Hobby hby = ihb.getdg(Integer.parseInt(hid));
			hy.add(hby);
		}
		//教员查询单个
		Theaher th = itb.getdg(Integer.parseInt(tid));
		//班级查询单个
		Class cl = icb.getdg(Integer.parseInt(cid));
		//调用增加的方法
		int n = isb.updStu(Integer.parseInt(sid), sname, cl, th, hy);
		
		if(n>0) {
			out.print("<script>alert('修改成功');location.href='index.jsp'</script>");
		}
		else {
			out.print("<script>alert('修改失败');location.href='XgServlet?sid="+sid+"'</script>");
		}
	}

}

修改界面代码:

<%@ 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 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>
	<center>
	<h2>修改界面</h2>
	<form action="UpdateServlet" method="post">
		<table>
			<tr>
				<td>学生编号</td>
				<td><input type="text" name="sid" value="${student1.sid }" readonly="readonly"></td>
			</tr>
			<tr>
				<td>学生姓名</td>
				<td><input type="text" name="sname" value="${student1.sname }" ></td>
			</tr>
			<tr>
				<td>学生教员</td>
				<td>
					<select name="tid">
					<c:forEach items="${theaher1 }" var="t">
						<option value="${t.tid }" <c:if test="${student1.th.tid==t.tid }">selected="selected"</c:if> >${t.tname }</option>
					</c:forEach>
					</select>
				</td>
			</tr>
			 <tr>
				<td>学生班级</td>
				<td>
					<select name="cid">
					<c:forEach items="${clas1 }" var="c">
						<option value="${c.cid }" <c:if test="${student1.cl.cid==c.cid }">selected="selected"</c:if> >${c.cname}</option>
					</c:forEach>
					</select>
				</td>
			</tr>
			<tr>
				<td>学生爱好</td>
				<td>
					<input type="checkbox" name="hid" value="1"  <c:if test="${aa.contains('1 ') }">checked</c:if>>篮球
					<input type="checkbox" name="hid" value="2"  <c:if test="${aa.contains('2 ') }">checked</c:if>>足球
					<input type="checkbox" name="hid" value="3"  <c:if test="${aa.contains('3 ') }">checked</c:if>>唱歌
					<input type="checkbox" name="hid" value="4"  <c:if test="${aa.contains('4 ') }">checked</c:if>>跳舞
				</td>
				<%-- <td>
				<c:forEach items="${hobby1 }" var="h">
					<input type="checkbox" name="hid" value="${h.hid }" <c:if test="${aa.contains('h.hid ')}">checked</c:if>>${h.hname }
				</c:forEach>	
				</td> --%>
			</tr>
			
		</table>
		<input type="submit" value="修改">
		<input type="reset" value="清空">
		</form>
		<a href="index.jsp">返回</a>
	</center>
</body>
</html>

 OK,今日的分享就到此结束啦,如果对个位看官有帮助的话可以留下免费的赞哦(收藏或关注也行),如果文章中有什么问题或不足以及需要改正的地方可以私信博主,博主会做出改正的。个位看官,小陽在此跟大家说拜拜啦!

  • 8
    点赞
  • 49
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

歐陽。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值