基础学生表增删改查

目录:

数据库展示

绑值与分页

查看

删除

增加

修改


数据库展示:

学生表:

列:学生id 学生姓名 年龄 性别 爱好编号 班级编号 教员编号

(展示时根据id拿到对应的值)

班级表:

列:班级id 班级名称

 教师表:

列:教师id 教师名称

爱好表:

列:爱好id 爱好名称

先连接一波数据库:

两个文件:

DBHelper:数据库辅助类:

package com.ljj.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("数据库连接(关闭)成功");
	}
}

数据库连接万能配置文件: 

#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/t280?useUnicode=true&characterEncoding=UTF-8&useSSL=false
user=root
pwd=123456



 各大包各大层:

绑值与分页:

需要用到的类与方法:

数据库访问层学生dao方法接口:

package com.ljj.dao;

import java.util.List;

import com.ljj.entity.Cclass;
import com.ljj.entity.Student;
import com.ljj.entity.Teacher;
import com.ljj.entity.loves;

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, Cclass cl, Teacher th, List<loves> 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, Cclass cl, Teacher th, List<loves> hy);
	
	
	
	/**
	 * 总行数
	 * @param bid 班级编号
	 * @param jid 教员id
	 * @param aid 爱好id
	 * @return
	 */
	public int count(String bid,String jid,String aid);
	
}

 数据库访问层学生dao方法实现类:

package com.ljj.dao;

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

import com.ljj.biz.LoveDaoBizimpl;
import com.ljj.entity.Cclass;
import com.ljj.entity.Student;
import com.ljj.entity.Teacher;
import com.ljj.entity.loves;
import com.ljj.util.DBHelper;
import com.mysql.jdbc.Statement;

public class StuDao implements IStuDao {

	private Connection conn=null;
	private Statement stmt=null;
	private ResultSet rs=null;
	
	//爱好
			LoveDaoBizimpl ldb = new LoveDaoBizimpl();
			
			@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.love,c.cname,d.tname from student a,loves b,class c,teacher d where a.cid=c.cid and a.tid=d.tid and a.lid = b.lid\r\n" + 
							")q  "+bid+"\r\n" + 
							")w "+jid+"\r\n" + 
							")e "+hid+"\r\n" + 
							")r limit "+a+","+pageSize+"";
					System.out.println(sql);
					//执行sql语句
					stmt = (Statement) conn.prepareStatement(sql);
					//获得结果集
					rs = stmt.executeQuery(sql);
					//遍历结果及
					while(rs.next()) {
						List<loves> hy = new ArrayList<>();
						String str = rs.getString("lid");
						String[] split = str.split(",");
						for (String hids : split) {
							//查询单个
							loves hby = ldb.getdg(Integer.parseInt(hids));
							hy.add(hby);
						}
						//班级
						Cclass cl = new Cclass(rs.getInt("cid"),rs.getString("cname"));
						//教员
						Teacher th = new Teacher(rs.getInt("tid"),rs.getString("tname"),cl);
						//学生
						Student stu = new Student(rs.getInt("id"),rs.getString("sname"),cl,th,hy);
						//加到集合中
						ls.add(stu);
					}
					
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					DBHelper.close(conn, stmt, rs);
				}
				return ls;
			}

			public static void main(String[] args) {
				System.out.println(new StuDao().getAll("", "", "", 1, 2));
			}
			@Override
			public Student getdg(int aid) {
				Student stu = new Student();
				try {
					//连接数据库
					conn = DBHelper.getConnection();
					//定义sql语句
					String sql = "select * from (\r\n" + 
							"select a.*,b.love,c.cname,d.tname from student a,loves b,class c,teacher d where a.cid=c.cid and a.tid=d.tid and a.lid = b.lid ) e where e.id="+aid+"";
					//执行sql语句
					stmt = (Statement) conn.prepareStatement(sql);
					//获得结果集
					rs = stmt.executeQuery(sql);
					//遍历结果及
					while(rs.next()) {
						List<loves> hy = new ArrayList<>();
						String str = rs.getString("lid");
						String[] split = str.split(",");
						for (String hid : split) {
							//查询单个
							loves hby = ldb.getdg(Integer.parseInt(hid));
							hy.add(hby);
						}
						//班级
						Cclass cl = new Cclass(rs.getInt("cid"),rs.getString("cname"));
						//教员
						Teacher th = new Teacher(rs.getInt("tid"),rs.getString("tname"),cl);
						//学生
						stu = new Student(rs.getInt("id"),rs.getString("sname"),cl,th,hy);
					}
					
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					DBHelper.close(conn, stmt, rs);
				}
				return stu;
			}

		

			@Override
			public int delStu(int xid) {
				int n = 0;
				try {
					//连接数据库
					conn = DBHelper.getConnection();
					//定义sql语句
					String sql = "delete from student where id="+xid+"";
					//执行sql语句
					stmt = (Statement) conn.prepareStatement(sql);
					//返回执行的行数
					n = stmt.executeUpdate(sql);
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					DBHelper.close(conn, stmt, 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.love,c.cname,d.tname from student a,loves b,class c,teacher d where a.cid=c.cid and a.tid=d.tid and a.lid = b.lid\r\n" + 
							")q  "+cid+" \r\n" + 
							")w  "+tid+" \r\n" + 
							")e  "+hid+" \r\n" + 
							")t";
//					System.out.println(sql);
					//执行sql语句
					stmt = (Statement) conn.prepareStatement(sql);
					//结果集
					rs = stmt.executeQuery(sql);
					if(rs.next()) {
						//返回执行的行数
						n = rs.getInt(1);
					}
				} catch (Exception e) {
					e.printStackTrace();
				} finally {
					DBHelper.close(conn, stmt, rs);
				}
				return n;
			}

	
		
}

数据库访问层教员dao方法接口:

package com.ljj.dao;

import java.util.List;

import com.ljj.entity.Teacher;

/**
 * 教师dao方法接口
 * @author Administrator 刘俊杰
 *下午7:31:01
 */
public interface ITeacherDao {

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

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

数据库访问层dao方法实现类:

package com.ljj.dao;

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

import com.ljj.entity.Cclass;
import com.ljj.entity.Teacher;
import com.ljj.entity.loves;
import com.ljj.util.DBHelper;
import com.mysql.jdbc.Statement;

public class TeacherDao implements ITeacherDao {
	
	//扩大作用域
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;

		
		@Override
		public List<Teacher> getAll() {
			List<Teacher> ls = new ArrayList<Teacher>();
			try {
				//连接数据库
				conn = DBHelper.getConnection();
				//定义sql语句
				String sql = "select * from teacher";
				//执行sql语句
				stmt = (Statement) conn.prepareStatement(sql);
				//获得结果集
				rs = stmt.executeQuery(sql);
				//遍历结果集
				while(rs.next()) {
					//实例化班级
					Cclass cl = new Cclass(rs.getInt(1),rs.getString(2));
					//实例化
					Teacher th = new Teacher(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 Teacher getdg(int tid) {
			//教师
			Teacher th = new Teacher();
			try {
				//连接数据库
				conn = DBHelper.getConnection();
				//定义sql语句
				String sql = "select * from (\r\n" + 
						"select t.*,c.name from teacher t,class c where t.cid = c.cid\r\n" + 
						")a  where a.tid = "+tid+"";
				//执行sql语句
				stmt = (Statement) conn.prepareStatement(sql);
				//获得结果集
				rs = stmt.executeQuery(sql);
				//遍历结果集
				while(rs.next()) {
					//实例化班级
					Cclass cl = new Cclass(rs.getInt("bid"),rs.getString("bname"));
					//实例化
					th = new Teacher(rs.getInt("jid"),rs.getString("jname"),cl);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				DBHelper.close(conn, stmt, rs);
			}
			return th;

		}

}

数据库访问层班级dao方法接口:

package com.ljj.dao;

import java.util.List;

import com.ljj.entity.Cclass;

/**
 * class接口
 * @author Administrator 刘俊杰
 *下午7:04:04
 */
public interface IClassDao  {
	/**
	 * 查询全部
	 * @return 班级集合
	 */
	public List<Cclass> getAll();
	
	/**
	 * 查询单个
	 * @param cid 班级编号
	 * @return 班级对象
	 */
	public Cclass getdg(int cid);




}

数据库访问层班级Dao方法实现类:

package com.ljj.dao;

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

import com.ljj.entity.Cclass;
import com.ljj.util.DBHelper;
import com.mysql.jdbc.Statement;
import com.sun.crypto.provider.RSACipher;

public class ClassDao implements IClassDao {

	private	Connection conn=null;
	private Statement stmt=null;
	private ResultSet rs=null;
	

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

	}


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

	}

	
	
}

数据库访问层爱好Dao方法接口:

package com.ljj.dao;

import java.util.List;

import com.ljj.entity.loves;

/**
 * 爱好dao方法 接口
 * @author Administrator 刘俊杰
 *下午4:41:28
 */
public interface IloveDao {

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

数据库访问层爱好Dao方法实现类:

package com.ljj.dao;

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

import com.ljj.entity.loves;
import com.ljj.util.DBHelper;
import com.mysql.jdbc.Statement;

public class LoveDao implements IloveDao {
	//扩大作用域
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;

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


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

以上是数据库访问层部分

业务逻辑层:

dao方法接口:(与数据库访问层的各种dao方法接口一致)

实现类:

业务逻辑层学生dao方法实现类:

package com.ljj.biz;

import java.util.List;

import com.ljj.dao.StuDao;
import com.ljj.entity.Cclass;
import com.ljj.entity.Student;
import com.ljj.entity.Teacher;
import com.ljj.entity.loves;

public class StuDaoBizimpl implements IStuDaoBiz {

	StuDao sd=new StuDao();

	@Override
	public List<Student> getAll(String cid, String tid, String hid, int pageIndex, int pageSize) {
		// TODO Auto-generated method stub
		return sd.getAll(cid, tid, hid, pageIndex, pageSize);
	}

	@Override
	public Student getdg(int aid) {
		// TODO Auto-generated method stub
		return sd.getdg(aid);
	}

	@Override
	public int addStu(String xname, Cclass cl, Teacher th, List<loves> hy) {
		// TODO Auto-generated method stub
		return sd.addStu(xname, cl, th, hy);
	}

	@Override
	public int delStu(int xid) {
		// TODO Auto-generated method stub
		return sd.delStu(xid);
	}

	@Override
	public int updStu(int xid, String xname, Cclass cl, Teacher th, List<loves> hy) {
		// TODO Auto-generated method stub
		return sd.updStu(xid, xname, cl, th, hy);
	}

	@Override
	public int count(String bid, String jid, String aid) {
		// TODO Auto-generated method stub
		return sd.count(bid, jid, aid);
	}

}

其他的类的dao方法业务逻辑层实现类与此类一致:实例数据库访问层然后用他来点击对应的方法给予给返回的值:

以上为业务逻辑层

实体类:按照数据库的数据与列名进行实体化,封装属性并重写toString方法,重写无参的构造方法与有参的构造方法

例如:学生类:

package com.ljj.entity;

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

public class Student implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int id;
	private String sname;//学生名称
	private Cclass cl;//班级对象
	private Teacher t;//教员对象
	private List<loves> love;//爱好集合
	
	public Student() {
		// TODO Auto-generated constructor stub
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public Cclass getCl() {
		return cl;
	}
	public void setCl(Cclass cl) {
		this.cl = cl;
	}
	public Teacher getT() {
		return t;
	}
	public void setT(Teacher t) {
		this.t = t;
	}
	public List<loves> getLove() {
		return love;
	}
	public void setLove(List<loves> love) {
		this.love = love;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public Student(int id, String sname, Cclass cl, Teacher t, List<loves> love) {
		super();
		this.id = id;
		this.sname = sname;
		this.cl = cl;
		this.t = t;
		this.love = love;
	}

	public Student( String sname, Cclass cl, Teacher t, List<loves> love) {
		this.sname = sname;
		this.cl = cl;
		this.t = t;
		this.love = love;
	}
	@Override
	public String toString() {
		return "Student [id=" + id + ", sname=" + sname + ", cl=" + cl + ", t=" + t + ", love=" + love + "]";
	}
	
	
}

主页面绑值与分页:

IndexServlet类:绑值与分页

package com.ljj.servelt;

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.ljj.biz.ClassDaoBizimpl;
import com.ljj.biz.IStuDaoBiz;
import com.ljj.biz.LoveDaoBizimpl;
import com.ljj.biz.StuDaoBizimpl;
import com.ljj.biz.TeacherDaoBizimpl;
import com.ljj.entity.Cclass;
import com.ljj.entity.Student;
import com.ljj.entity.Teacher;
import com.ljj.entity.loves;


/**
 * Servlet implementation class IndexServlet
 */
@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="";
		}
		System.out.println(cid);
		String tid= request.getParameter("jid");
		if(tid==null) {
			tid="";
		}
		System.out.println(tid);
		String[] hids = request.getParameterValues("hid");
		System.out.println(hids);
		
		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 lid like '%"+hidd+"%' ";
					}
				}
				else {
					if(str==null) {
						System.out.println("按市场2");
						hidd+=str+",";//用空格进行隔开
						hid= " where lid 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);
		}
		
		
		//业务逻辑层
		StuDaoBizimpl isb = new StuDaoBizimpl();
		LoveDaoBizimpl ihb = new LoveDaoBizimpl();//爱好
		TeacherDaoBizimpl itb = new TeacherDaoBizimpl();//教员
		 ClassDaoBizimpl icb = new ClassDaoBizimpl();//班级

		//调用查询全部的方法
		List<Student> stu = isb.getAll(cid, tid,hid, pageIndex, pageSize);
		List<loves> hobby12 = ihb.getAll();
		List<Teacher> theaher12 = itb.getAll();
		List<Cclass> 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("loves", hobby12);
			request.setAttribute("teacher", 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("集合为空");
		}
		
	}

}

主界面:index.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>
</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="${teacher }" 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.name}</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.id }</td>
				<td>${s.sname}</td>
				<td>${s.t.tname}</td>
				<td>${s.cl.name}</td>
				<td>
					<c:forEach items="${s.love}" var="x">
						${x.love}
					</c:forEach>
				</td> 
				<td>
					<a href="CkServlet?sid=${s.id }">查看</a>
					<a href="XgServlet?sid=${s.id }">修改</a>
					<a  onclick="return confirm('你确定要删除吗?')"  href="DeleteServlet?sid=${s.id }">删除</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>

 点击下一页进入第二页,或者手动输入页数点击go即可跳转过去

查看

查看Servlet类:CkServlet

package com.ljj.servelt;

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.ljj.biz.ClassDaoBizimpl;
import com.ljj.biz.LoveDaoBizimpl;
import com.ljj.biz.StuDaoBizimpl;
import com.ljj.biz.TeacherDaoBizimpl;
import com.ljj.entity.Cclass;
import com.ljj.entity.Student;
import com.ljj.entity.Teacher;
import com.ljj.entity.loves;

/**
 * Servlet implementation class CkServlet
 */
@WebServlet("/CkServlet")
public class CkServlet 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");
		System.out.println(sid+"fsjdalf");
		// 业务逻辑层
		LoveDaoBizimpl ihb = new LoveDaoBizimpl();// 爱好
		TeacherDaoBizimpl itb = new TeacherDaoBizimpl();// 教员
		StuDaoBizimpl icb = new StuDaoBizimpl();// 学生
		ClassDaoBizimpl isb = new ClassDaoBizimpl();// 班级

		// 调用查询全部的方法
		List<loves> hobby2 = ihb.getAll();
		List<Teacher> theaher2 = itb.getAll();
		Student student2 = icb.getdg(Integer.parseInt(sid));
		System.out.println(student2);
		List<Cclass> clas2 = isb.getAll();
		List<loves> hy = student2.getLove();
		String bb = "";
		for (loves hobby : hy) {
			int hid = hobby.getLid();
			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("ck.jsp").forward(request, response);
		} else {
			System.out.println("集合为空");
		}
	}
	
}

 查看jsp页面:Ck.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 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>
	<form action="#" method="post">
		<table>
			<tr>
				<td>学生编号</td>
				<td><input type="text" name="sid" value="${student2.id }" 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.t.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.name}</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>>吃s
					<input type="checkbox" name="hid" value="3"  <c:if test="${bb.contains('3 ') }">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:deleteServlet:

package com.ljj.servelt;

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.ljj.biz.StuDaoBizimpl;

/**
 * Servlet implementation class DeleteServlet
 */
@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");
		//业务逻辑
		StuDaoBizimpl isb = new StuDaoBizimpl();
		//删除
		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>");
		}
	}

}

点击主界面删除:出现提示

删除后:

修改:

修改前的绑值:XgServlet:

package com.ljj.servelt;

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.ljj.biz.ClassDaoBizimpl;
import com.ljj.biz.LoveDaoBizimpl;
import com.ljj.biz.StuDaoBizimpl;
import com.ljj.biz.TeacherDaoBizimpl;
import com.ljj.entity.Cclass;
import com.ljj.entity.Student;
import com.ljj.entity.Teacher;
import com.ljj.entity.loves;

/**
 * Servlet implementation class XgServlet
 */
@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");

		//业务逻辑层
		LoveDaoBizimpl ihb = new LoveDaoBizimpl();//爱好
		TeacherDaoBizimpl itb = new TeacherDaoBizimpl();//教员
		ClassDaoBizimpl icb = new ClassDaoBizimpl();//班级
		StuDaoBizimpl isb = new StuDaoBizimpl();//学生
		
		//调用查询全部的方法
		List<loves> hobby1 = ihb.getAll();
		List<Teacher> theaher1 = itb.getAll();
		List<Cclass> clas1 = icb.getAll();
		Student student1 = isb.getdg(Integer.parseInt(sid));
//		Student student1 = isb.getdg(3);
		List<loves> hy = student1.getLove();
		String aa = "";
		for (loves hobby : hy) {
			int hid = hobby.getLid();
			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("集合为空");
		}
	}
}

修改界面:update.sjp

<%@ 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>
	<form action="UpdateServlet" method="post">
		<table>
			<tr>
				<td>学生编号</td>
				<td><input type="text" name="sid" value="${student1.id }" 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.t.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.name}</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>>吃s
					<input type="checkbox" name="hid" value="3"  <c:if test="${aa.contains('3 ') }">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>
		
	</center>
</body>
</html>

修改传值UpdateServlet:

package com.ljj.servelt;

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.ljj.biz.ClassDaoBizimpl;
import com.ljj.biz.LoveDaoBizimpl;
import com.ljj.biz.StuDaoBizimpl;
import com.ljj.biz.TeacherDaoBizimpl;
import com.ljj.entity.Cclass;
import com.ljj.entity.Teacher;
import com.ljj.entity.loves;

/**
 * Servlet implementation class UpdateServlet
 */
@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();
		
		//业务逻辑层
		LoveDaoBizimpl ihb = new LoveDaoBizimpl();//爱好
		TeacherDaoBizimpl itb = new TeacherDaoBizimpl();//教员
		ClassDaoBizimpl icb = new ClassDaoBizimpl();//班级
		StuDaoBizimpl isb = new StuDaoBizimpl();//学生
		
		List<loves> hy = new ArrayList<>();
		String[] split = hsy.split(" ");
		for (String hid : split) {
			//查询单个
			loves hby = ihb.getdg(Integer.parseInt(hid));
			hy.add(hby);
		}
		//教员查询单个
		Teacher th = itb.getdg(Integer.parseInt(tid));
		//班级查询单个
		Cclass 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>");
		}
	}

}

界面展示:点击修改进入修改界面:

 点击修改3号学生,编号不可修改(文本框禁止编辑) 

修改前绑值成功:

修改后界面:

 增加:

增加前下拉框绑值:

package com.ljj.servelt;

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.ljj.biz.ClassDaoBizimpl;
import com.ljj.biz.LoveDaoBizimpl;
import com.ljj.biz.TeacherDaoBizimpl;
import com.ljj.entity.Cclass;
import com.ljj.entity.Teacher;
import com.ljj.entity.loves;

/**
 * Servlet implementation class AddqServlet
 */
@WebServlet("/AddqServlet")
public class AddqServlet 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");

		//业务逻辑层
		LoveDaoBizimpl ihb = new LoveDaoBizimpl();//爱好
		TeacherDaoBizimpl itb = new TeacherDaoBizimpl();//教员
		ClassDaoBizimpl icb = new ClassDaoBizimpl();//班级
		
		//调用查询全部的方法
		List<loves> hobby = ihb.getAll();
		System.out.println(hobby);
		List<Teacher> theaher = itb.getAll();
		System.out.println(theaher);
		List<Cclass> clas = icb.getAll();
		System.out.println(clas);
		
		//判断
		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("集合为空");
		}
		
	}
}

 增加Servlet:AddServlet:

package com.ljj.servelt;

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.ljj.biz.ClassDaoBizimpl;
import com.ljj.biz.LoveDaoBizimpl;
import com.ljj.biz.StuDaoBizimpl;
import com.ljj.biz.TeacherDaoBizimpl;
import com.ljj.entity.Cclass;
import com.ljj.entity.Teacher;
import com.ljj.entity.loves;

/**
 * Servlet implementation class AddServlet
 */
@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");
		System.out.println(sname+"--------"+tid+"-----"+cid+"------"+hids);
		String hsy = "";// 用来拼接
		for (String str : hids) {
			hsy += str + " ";// 用空格进行隔开
		}

		PrintWriter out = response.getWriter();

		// 业务逻辑层
		TeacherDaoBizimpl itb = new TeacherDaoBizimpl();
		ClassDaoBizimpl icb = new ClassDaoBizimpl();
		LoveDaoBizimpl ihb = new LoveDaoBizimpl();
		StuDaoBizimpl isb = new StuDaoBizimpl();

		List<loves> hy = new ArrayList<>();
		String[] split = hsy.split(" ");
		for (String hid : split) {
			// 查询单个
			loves hby = ihb.getdg(Integer.parseInt(hid));
			hy.add(hby);
		}
		// 教员查询单个
		Teacher th = itb.getdg(Integer.parseInt(tid));
		System.out.println("教师:"+th);
		// 班级查询单个
		Cclass 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>");
		}

	}

}

增加界面:add.jsp

<%@ 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>
	<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.name}</option>
					</c:forEach>
					</select>
				</td>
			</tr>
			<tr>
				<td>学生爱好</td>
				<td>
				<c:forEach items="${hobby }" var="h">
					<input type="checkbox" name="hid" value="${h.lid }">${h.love }
				</c:forEach>	
				</td>
			</tr>
			
		</table>
		<input type="submit" value="确定">
		<input type="reset" value="清空">
		</form>
		
	</center>
</body>
</html>

界面展示:

增加后:

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值