文章目录
一、所用工具
二、数据库界面
三、底层代码
util包--连接数据库
entity包 --实体类
dao --方法类
学生 教师 爱好 班级
biz --业务逻辑层
学生 教师 爱好 班级
servlet
绑值&模糊查询 增加 修改 删除
四、界面
index.jsp主界面
add.jsp增加
update.jsp修改
嗨~
数据库:MySQL
开发工具:eclipse
系统需求:1️⃣使用MySQL建立教员表、学生表、班级表、爱好表;
2️⃣要求带条件进行查询(使用下拉框&复选框&无刷新分页);
3️⃣新增功能;
4️⃣修改功能(界面样式可与新增界面一致,但教员以及教员所在班级的下拉框,要求默认选中对应的值,学生的爱好有哪些,那么默认勾选上对应的爱好复选框);
5️⃣删除功能,删除带提示
班级表:
爱好表:
学生表:
教师表:
#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/0614?useUnicode=true&characterEncoding=UTF-8&useSSL=false
user=root
pwd=123456
package com.hemingxiang.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.hemingxiang.entity;
import java.io.Serializable;
/**
* 实体类:教师类
* @author hmx
*
*/
public class Teacher implements Serializable{
//防止序列化与反序列化
private static final long serialVersionUID = 1L;
private int tid;//教员编号
private String tname;//教员姓名
private Class c;//所教班级
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 getC() {
return c;
}
public void setC(Class c) {
this.c = c;
}
public Teacher() {
// TODO Auto-generated constructor stub
}
public Teacher(int tid, String tname, Class c) {
this.tid = tid;
this.tname = tname;
this.c = c;
}
public Teacher( String tname, Class c) {
this.tname = tname;
this.c = c;
}
@Override
public String toString() {
return "Teacher [tid=" + tid + ", tname=" + tname + ", c=" + c + "]";
}
}
package com.hemingxiang.entity;
import java.io.Serializable;
import java.util.List;
/**
* 实体类:学生类
* @author hmx
*
*/
public class Student implements Serializable{
//防止序列化与反序列化
private static final long serialVersionUID = 1L;
private int sid;//学生编号
private String sname;//学生姓名
private Class c;//班级
private Habby h;//爱好
private Teacher tea;//老师
private String ss;
private List ls;
public String getSs() {
return ss;
}
public void setSs(String ss) {
this.ss = ss;
}
public List getLs() {
return ls;
}
public void setLs(List ls) {
this.ls = ls;
}
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 getC() {
return c;
}
public void setC(Class c) {
this.c = c;
}
public Habby getH() {
return h;
}
public void setH(Habby h) {
this.h = h;
}
public Teacher getTea() {
return tea;
}
public void setTea(Teacher tea) {
this.tea = tea;
}
public Student() {
// TODO Auto-generated constructor stub
}
public Student(int sid, String sname, Class c, Habby h, Teacher tea, String ss, List ls) {
this.sid = sid;
this.sname = sname;
this.c = c;
this.h = h;
this.tea = tea;
this.ss = ss;
this.ls = ls;
}
public Student(int sid, String sname, Class c,Teacher tea, String ss) {
this.sid = sid;
this.sname = sname;
this.c = c;
this.tea = tea;
this.ss = ss;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", c=" + c + ", h=" + h + ", tea=" + tea + ", ss=" + ss
+ ", ls=" + ls + "]";
}
}
package com.hemingxiang.entity;
import java.io.Serializable;
/**
* 实体类:爱好类
* @author hmx
*
*/
public class Habby implements Serializable{
//防止序列化与反序列化
private static final long serialVersionUID = 1L;
private String hid;//爱好编号
private String hname;//爱好名称
public String getHid() {
return hid;
}
public void setHid(String hid) {
this.hid = hid;
}
public String getHname() {
return hname;
}
public void setHname(String hname) {
this.hname = hname;
}
public Habby() {
// TODO Auto-generated constructor stub
}
public Habby(String hid, String hname) {
this.hid = hid;
this.hname = hname;
}
public Habby( String hname) {
this.hname = hname;
}
@Override
public String toString() {
return "Habby [hid=" + hid + ", hname=" + hname + "]";
}
}
package com.hemingxiang.entity;
import java.io.Serializable;
/**
* 实体类:班级类
* @author hmx
*
*/
public class Class implements Serializable{
//防止序列化与反序列化
private static final long serialVersionUID = 1L;
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;
}
public Class(String cname) {
this.cname = cname;
}
@Override
public String toString() {
return "Class [cid=" + cid + ", cname=" + cname + "]";
}
}
package com.hemingxiang.dao;
import java.util.List;
import com.cdl.entity.Student;
/**
* 数据库访问层接口
* @author hmx
*
*/
public interface IStuDao {
/**
* 查询单个
* @param sid 学生编号
* @return 学生对象
*/
public Student getStu(int sid) ;
/**
* 查询所有
* @return 查询的结果集合
*/
public List getAll();
/**
* 模糊查询
* @param colName 要查询的列名
* @param str 关键字
* @return 对象
*/
public List getMH(String ctr,String htr, String str,int pageIndex,int pageSize);
/**
* 增加学生
* @param stu 学生对象
* @return 影响行数
*/
public int addStu(Student stu);
/**
* 删除学生
* @param sid 学生编号
* @return 影响行数
*/
public int delStu(int sid);
/**
* 修改学生
* @param stu 要修改的对象
* @param sid 学生编号
* @return 影响行数
*/
public int upStu(Student stu,int sid);
/**
* 总行数
* @return 行数
*/
public int getRows(String str);
}
package com.hemingxiang.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.hemingxiang.entity.Class;
import com.hemingxiang.entity.Habby;
import com.hemingxiang.entity.Student;
import com.hemingxiang.util.DBHelper;
/**
* 数据库访问层
* @author hmx
*
*/
public class StuDao implements IStuDao{
//三兄弟
Connection con = null;
PreparedStatement ps =null;
ResultSet rs = null;
public Student getStu(int sid) {
Student stu = new Student();
try {
//获得连接
con = DBHelper.getConnection();
//定义sql语句
String sql = "select * from tb_stu where sid=?";
//获得执行对象
ps=con.prepareStatement(sql);
//给占位符赋值
ps.setInt(1, sid);
//给rs赋值
rs=ps.executeQuery();
//判断
if(rs.next()) {
stu.setSid(rs.getInt(1));
stu.setSname(rs.getString(2));
stu.setC(new ClassDao().getClz(rs.getInt(3)));
//stu.setH(new HobbyDao().getHob(rs.getInt(4)));
stu.setSs(rs.getString(4));
stu.setTea(new TeaDao().getTea(rs.getInt(5)));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
DBHelper.close(con, ps, rs);
}
return stu;
}
@Override
public List getAll() {
List ls = new ArrayList();
try {
//获得连接
con = DBHelper.getConnection();
//定义sql语句
String sql = "select * from tb_stu";
//获得执行对象
ps=con.prepareStatement(sql);
//给集合赋值
rs=ps.executeQuery();
//遍历
while(rs.next()) {
List list = new ArrayList<>();
//实例化一个对象
Student stu = new Student();
stu.setSid(rs.getInt(1));
stu.setSname(rs.getString(2));
stu.setC(new ClassDao().getClz(rs.getInt(3)));
//stu.setH(new HobbyDao().getHob(rs.getInt(4)));
//分割
String hob = rs.getString(4);
//System.out.println(rs.getString(4));
//用逗号分割
String [] ss = hob.split(",");
for (String so : ss) {
IHobbyDao ihd = new HobbyDao();
Habby h = ihd.getHob(Integer.parseInt(so));
list.add(h);
}
stu.setLs(list);
stu.setTea(new TeaDao().getTea(rs.getInt(5)));
//加进去
ls.add(stu);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally {
DBHelper.close(con, ps, rs);
}
return ls;
}
@Override
public List getMH(String ctr, String htr,String str,int pageIndex,int pageSize) {
List ls = new ArrayList<>();
int a = (pageIndex-1)*pageSize;
int b = pageSize;
try {
//获得连接
con = DBHelper.getConnection();
//定义sql语句
String sql = "select * from tb_stu where hid like '%"+htr+"%' and tid like '%"+str+"%' and cid like '%"+ctr+"%' limit ?,?";
//获得执行对象
ps=con.prepareStatement(sql);
ps.setInt(1, a);
ps.setInt(2, b);
//给集合赋值
rs=ps.executeQuery();
//遍历
if(rs.next()) {
List list = new ArrayList<>();
//实例化一个对象
Student stu = new Student();
stu.setSid(rs.getInt(1));
stu.setSname(rs.getString(2));
stu.setC(new ClassDao().getClz(rs.getInt(3)));
//stu.setH(new HobbyDao().getHob(rs.getInt(4)));
//分割
String hob = rs.getString(4);
//System.out.println(rs.getString(4));
//用逗号分割
String [] ss = hob.split(",");
for (String so : ss) {
IHobbyDao ihd = new HobbyDao();
//调用单个查询方法
Habby h = ihd.getHob(Integer.parseInt(so));
list.add(h);
}
stu.setLs(list);
stu.setTea(new TeaDao().getTea(rs.getInt(5)));
//加进去
ls.add(stu);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally {
DBHelper.close(con, ps, rs);
}
return ls;
}
@Override
public int addStu(Student stu) {
int n = 0;
try {
//获得连接
con = DBHelper.getConnection();
//定义sql语句
String sql = "insert into tb_stu(sid,sname,cid,hid,tid) values(?,?,?,?,?)";
//获得执行对象
ps=con.prepareStatement(sql);
//给占位符赋值
ps.setInt(1, stu.getSid());
ps.setString(2, stu.getSname());
ps.setInt(3,stu.getC().getCid());
ps.setString(4, stu.getSs());
ps.setInt(5,stu.getTea().getTid());
//给n赋值
n=ps.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
DBHelper.close(con, ps, rs);
}
return n;
}
@Override
public int delStu(int sid) {
int n = 0;
try {
//获得连接
con = DBHelper.getConnection();
//定义sql语句
String sql = "delete from tb_stu where sid=?";
//获得执行对象
ps=con.prepareStatement(sql);
//给占位符赋值
ps.setInt(1, sid);
//给n赋值
n=ps.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
DBHelper.close(con, ps, rs);
}
return n;
}
@Override
public int upStu(Student stu, int sid) {
int n = 0;
try {
//获得连接
con = DBHelper.getConnection();
//定义sql语句
String sql = "update tb_stu set sname=?,cid=?,hid=?,tid=? where sid=?";
//获得执行对象
ps=con.prepareStatement(sql);
//给占位符赋值
ps.setString(1, stu.getSname());
ps.setInt(2, stu.getC().getCid());
ps.setString(3, stu.getSs());
ps.setInt(4, stu.getTea().getTid());
ps.setInt(5, sid);
//给n赋值
n=ps.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
DBHelper.close(con, ps, rs);
}
return n;
}
@Override
public int getRows(String str) {
int n = 0;
try {
con = DBHelper.getConnection();
String sql = "select count(*) from ?";
ps=con.prepareStatement(sql);
ps.setString(1, str);
rs=ps.executeQuery();
if(rs.next()) {
n=rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.close(con, ps, null);
}
return n;
}
public static void main(String[] args) {
StuDao stu = new StuDao();
// System.out.println(123);
/*List sd = stu.getAll();
for (Student student : sd) {
System.out.println(student);
}*/
/*Student stu2 = stu.getStu(3);
System.out.println(stu2);*/
/*List mh = stu.getMH("sname", "三", 2, 2);
for (Student student : mh) {
System.out.println(student+"=====================");
}*/
List mh = stu.getMH("278", "1", "2202", 1, 2);
for (Student student : mh) {
System.out.println(student+"=================");
}
}
}
package com.hemingxiang.dao;
import java.util.List;
import com.hemingxiang.entity.Teacher;
/**
* 数据访问层接口类
* @author hmx
*
*/
public interface ITeacDao {
/**
* 查询单个教员
* @param tid 教员编号
* @return 教员对象
*/
public Teacher getTea(int tid);
/**
* 查询所有教员
* @return
*/
public List getAll();
}
package com.hemingxiang.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.hemingxiang.entity.Teacher;
import com.hemingxiang.utils.DBHelper;
/**
* 数据库访问层
* @author hmx
*
*/
public class TeaDao implements ITeacDao{
//三兄弟
Connection con = null;
PreparedStatement ps =null;
ResultSet rs = null;
@Override
public Teacher getTea(int tid) {
Teacher tea = new Teacher();
try {
//获得连接
con = DBHelper.getConnection();
//定义sql语句
String sql = "select * from tb_teacher where tid=?";
//获得执行对象
ps=con.prepareStatement(sql);
//给占位符赋值
ps.setInt(1, tid);
//给rs赋值
rs=ps.executeQuery();
//判断
if(rs.next()) {
tea.setTid(rs.getInt(1));
tea.setTname(rs.getString(2));
tea.setC(new ClassDao().getClz(3));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally {
DBHelper.close(con, ps, rs);
}
return tea;
}
@Override
public List getAll() {
List list = new ArrayList();
try {
//获得连接
con = DBHelper.getConnection();
//定义sql语句
String sql = "select * from tb_teacher";
//获得执行对象
ps=con.prepareStatement(sql);
//给rs赋值
rs=ps.executeQuery();
//判断
while(rs.next()) {
Teacher tea = new Teacher();
tea.setTid(rs.getInt(1));
tea.setTname(rs.getString(2));
tea.setC(new ClassDao().getClz(rs.getInt(3)));
//加进去
list.add(tea);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally {
DBHelper.close(con, ps, rs);
}
return list;
}
}
package com.hemingxiang.dao;
/**
* 数据库访问层接口类
* @author hmx
*
*/
import java.util.List;
import com.hemingxiang.entity.Habby;
public interface IHobbyDao {
/**
* 查询单个
* @param hid 爱好编号
* @return 爱好的对象
*/
public Habby getHob(int hid);
/**
* 查询所有
* @return 爱好的集合
*/
public List getAll();
}
package com.hemingxing.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.hemingxing.entity.Habby;
import com.hemingxing.util.DBHelper;
/**
* 数据库访问层
* @author hmx
*
*/
public class HobbyDao implements IHobbyDao{
//三兄弟
Connection con = null;
PreparedStatement ps =null;
ResultSet rs = null;
@Override
public Habby getHob(int hid) {
Habby h = new Habby();
try {
//获得连接
con = DBHelper.getConnection();
//定义sql语句
String sql = "select * from tb_habby where hid=?";
//获得执行对象
ps=con.prepareStatement(sql);
//给占位符赋值
ps.setInt(1, hid);
//给rs赋值
rs=ps.executeQuery();
//判断
if(rs.next()) {
h.setHid(rs.getString(1));
h.setHname(rs.getString(2));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
finally {
DBHelper.close(con, ps, rs);
}
return h;
}
@Override
public List getAll() {
List list = new ArrayList();
try {
//获得连接
con = DBHelper.getConnection();
//定义sql语句
String sql = "select * from tb_habby";
//获得执行对象
ps=con.prepareStatement(sql);
//给rs赋值
rs=ps.executeQuery();
//遍历
while(rs.next()) {
Habby h = new Habby();
h.setHid(rs.getString(1));
h.setHname(rs.getString(2));
//加进去
list.add(h);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
DBHelper.close(con, ps, rs);
}
return list;
}
}
package com.hemingxiang.dao;
/**
* 数据库访问层
* @author hmx
*
*/
import java.util.List;
import com.hemingxiang.entity.Class;
public interface IClassDao {
/**
* 查询单个班级
* @return 班级对象
*/
public Class getClz(int cid);
/**
* 查询所有
* @return 班级和集合
*/
public List getAll();
}
package com.hemingxiang.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.hemingxiang.entity.Class;
import com.hemingxiang.util.DBHelper;
/**
* 数据库访问层
* @author hmx
*
*/
public class ClassDao implements IClassDao{
//三兄弟
Connection con = null;
PreparedStatement ps =null;
ResultSet rs = null;
@Override
public Class getClz(int cid) {
Class clz = new Class();
try {
//获得连接
con = DBHelper.getConnection();
//定义sql语句
String sql = "select * from tb_class where cid=?";
//获得执行对象
ps=con.prepareStatement(sql);
//给占位符赋值
ps.setInt(1, cid);
//给rs赋值
rs=ps.executeQuery();
//判断
if(rs.next()) {
clz.setCid(rs.getInt(1));
clz.setCname(rs.getString(2));
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
DBHelper.close(con, ps, rs);
}
return clz;
}
@Override
public List getAll() {
List list = new ArrayList();
try {
//获得连接
con = DBHelper.getConnection();
//定义sql语句
String sql = "select * from tb_class";
//获得执行对象
ps=con.prepareStatement(sql);
//给rs赋值
rs=ps.executeQuery();
//遍历
while(rs.next()) {
Class c = new Class();
c.setCid(rs.getInt(1));
c.setCname(rs.getString(2));
//加进去
list.add(c);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
DBHelper.close(con, ps, rs);
}
return list;
}
}
package com.hemingxiang.biz;
import java.util.List;
import com.hemingxiang.entity.Student;
/**
* 业务逻辑层设计类
* @author hmx
*
*/
public interface IStuBiz {
/**
* 查询单个
* @param sid 学生编号
* @return 学生对象
*/
public Student getStu(int sid) ;
/**
* 查询所有
* @return 查询的结果集合
*/
public List getAll();
/**
* 模糊查询
* @param colName 要查询的列名
* @param str 关键字
* @return 对象
*/
public List getMH(String ctr,String htr, String str,int pageIndex,int pageSize);
/**
* 增加学生
* @param stu 学生对象
* @return 影响行数
*/
public int addStu(Student stu);
/**
* 删除学生
* @param sid 学生编号
* @return 影响行数
*/
public int delStu(int sid);
/**
* 修改学生
* @param stu 要修改的对象
* @param sid 学生编号
* @return 影响行数
*/
public int upStu(Student stu,int sid);
/**
* 总行数
* @return
*/
public int getRows(String str);
}
package com.hemingxiang.biz;
/**
* 业务逻辑层
* @author hmx
*
*/
import java.util.List;
import com.hemingxiang.dao.IStuDao;
import com.hemingxiang.dao.StuDao;
import com.hemingxiang.entity.Student;
public class StuBiz implements IStuBiz{
//调用数据库访问层
IStuDao isd = new StuDao();
@Override
public List getAll() {
// TODO Auto-generated method stub
return isd.getAll();
}
@Override
public int addStu(Student stu) {
// TODO Auto-generated method stub
return isd.addStu(stu);
}
@Override
public int delStu(int sid) {
// TODO Auto-generated method stub
return isd.delStu(sid);
}
@Override
public int upStu(Student stu, int sid) {
// TODO Auto-generated method stub
return isd.upStu(stu, sid);
}
@Override
public Student getStu(int sid) {
// TODO Auto-generated method stub
return isd.getStu(sid);
}
@Override
public List getMH(String ctr, String htr, String str, int pageIndex, int pageSize) {
// TODO Auto-generated method stub
return isd.getMH(ctr, htr, str, pageIndex, pageSize);
}
@Override
public int getRows(String str) {
// TODO Auto-generated method stub
return isd.getRows(str);
}
}
package com.hemingxiang.biz;
/**
* 业务逻辑层接口类
* @author hmx
*
*/
import java.util.List;
import com.hemingxiang.entity.Teacher;
public interface ITeacBiz {
/**
* 查询单个教员
* @param tid 教员编号
* @return 教员对象
*/
public Teacher getTea(int tid);
/**
* 查询所有教员
* @return
*/
public List getAll();
}
package com.hemingxiang.biz;
/**
* 业务逻辑层
* @author hmx
*
*/
import java.util.List;
import com.hemingxiang.dao.ITeacDao;
import com.hemingxiang.dao.TeaDao;
import com.hemingxiang.entity.Teacher;
public class TeaBiz implements ITeacBiz{
//调用数据库访问层
ITeacDao itd = new TeaDao();
@Override
public Teacher getTea(int tid) {
// TODO Auto-generated method stub
return itd.getTea(tid);
}
@Override
public List getAll() {
// TODO Auto-generated method stub
return itd.getAll();
}
}
package com.hemingxiang.biz;
/**
* 业务逻辑层接口类
* @author hmx
*
*/
import java.util.List;
import com.hemingxiang.entity.Habby;
public interface IHobbyBiz {
/**
* 查询单个
* @param hid 爱好编号
* @return 爱好的对象
*/
public Habby getHob(int hid);
/**
* 查询所有
* @return 爱好的集合
*/
public List getAll();
}
package com.hemingxiang.biz;
import java.util.List;
import com.hemingxiang.entity.Class;
/**
* 业务逻辑层接口类
* @author hmx
*
*/
public interface IClassBiz {
/**
* 查询单个班级
* @return 班级对象
*/
public Class getClz(int cid);
/**
* 查询所有
* @return 班级和集合
*/
public List getAll();
}
package com.hemingxiang.biz;
/***
* 业务逻辑层
* @author hmx
*
*/
import java.util.List;
import com.hemingxiang.dao.ClassDao;
import com.hemingxiang.dao.IClassDao;
import com.hemingxiang.entity.Class;
public class ClassBiz implements IClassBiz{
//调用数据库访问层
IClassDao icd = new ClassDao();
@Override
public Class getClz(int cid) {
// TODO Auto-generated method stub
return icd.getClz(cid);
}
@Override
public List getAll() {
// TODO Auto-generated method stub
return icd.getAll();
}
}
package com.hemingxiang.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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.hemingxiang.biz.ClassBiz;
import com.hemingxiang.biz.HobbyBiz;
import com.hemingxiang.biz.IClassBiz;
import com.hemingxiang.biz.IHobbyBiz;
import com.hemingxiang.biz.IStuBiz;
import com.hemingxiang.biz.ITeacBiz;
import com.hemingxiang.biz.StuBiz;
import com.hemingxiang.biz.TeaBiz;
import com.hemingxiang.entity.Class;
import com.hemingxiang.entity.Habby;
import com.hemingxiang.entity.Student;
import com.hemingxiang.entity.Teacher;
/**
* 主界面绑值以及模糊查询
*/
@WebServlet("/index.do")
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");
//获取out
PrintWriter out = response.getWriter();
//调用biz层
IStuBiz stb = new StuBiz();
ITeacBiz itb = new TeaBiz();
IClassBiz icb = new ClassBiz();
IHobbyBiz ihb = new HobbyBiz();
//定义页码页数
int pageIndex=1;
int pageSize=2;
//接收表单值
String cid = request.getParameter("ctr");//班级
String hid = request.getParameter("htr");//爱好
String tid = request.getParameter("str");//教员
if(tid==null) {
tid="";
}
if(hid==null) {
hid="";
}
if(cid==null) {
cid="";
}
String pid = request.getParameter("pid");
if(pid!=null) {
pageIndex=Integer.parseInt(pid);
}
//select * from tb_teacher where tid like '%"+hid+"%'
int rows = stb.getRows(" tb_stu where hid like '%"+hid+"%' and tid like '%"+tid+"%' and cid like '%"+cid+"%' ");
int max = rows/pageSize;
if(rows%pageSize!=0) {
max++;
}
if(max==0) {
max=1;
}
//调用方法
List mytea = itb.getAll();
List myclz = icb.getAll();
List myh = ihb.getAll();
List myls = stb.getMH(cid, hid, tid, pageIndex,pageSize);
// Student stu = new Student();
// List ls = stu.getLs();
// String aa = "";
// for (Habby habby : ls) {
// aa+=habby+",";
// }
/*int rows = */
request.setAttribute("mytea", mytea);
request.setAttribute("myclz", myclz);
request.setAttribute("myh", myh);
request.setAttribute("max", max);
request.setAttribute("pageIndex", pageIndex);
request.setAttribute("myls", myls);
//判断
request.getRequestDispatcher("index.jsp").forward(request, response);
/*if(myls!=null) {
}
else {
System.out.println("检查dao方法");
}*/
}
}
package com.hemingxiang.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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.hemingxiang.biz.ClassBiz;
import com.hemingxiang.biz.HobbyBiz;
import com.hemingxiang.biz.IClassBiz;
import com.hemingxiang.biz.IHobbyBiz;
import com.hemingxiang.biz.IStuBiz;
import com.hemingxiang.biz.ITeacBiz;
import com.hemingxiang.biz.StuBiz;
import com.hemingxiang.biz.TeaBiz;
import com.hemingxiang.entity.Class;
import com.hemingxiang.entity.Habby;
import com.hemingxiang.entity.Student;
import com.hemingxiang.entity.Teacher;
/**
* 增加前增加界面的绑值
*/
@WebServlet("/add1.do")
public class AddIndexServlet 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();
//调用biz层
IStuBiz stb = new StuBiz();
ITeacBiz itb = new TeaBiz();
IClassBiz icb = new ClassBiz();
IHobbyBiz ihb = new HobbyBiz();
//调用方法
List myls = stb.getAll();
List mytea = itb.getAll();
List myclz = icb.getAll();
List myh = ihb.getAll();
request.setAttribute("mytea", mytea);
request.setAttribute("myclz", myclz);
request.setAttribute("myh", myh);
//判断
if(myls!=null) {
request.setAttribute("myls", myls);
request.getRequestDispatcher("add.jsp").forward(request, response);
}
else {
System.out.println("检查dao方法");
}
}
}
package com.hemingxiang.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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.hemingxiang.biz.ClassBiz;
import com.hemingxiang.biz.HobbyBiz;
import com.hemingxiang.biz.IClassBiz;
import com.hemingxiang.biz.IHobbyBiz;
import com.hemingxiang.biz.IStuBiz;
import com.hemingxiang.biz.ITeacBiz;
import com.hemingxiang.biz.StuBiz;
import com.hemingxiang.biz.TeaBiz;
import com.hemingxiang.entity.Class;
import com.hemingxiang.entity.Habby;
import com.hemingxiang.entity.Student;
import com.hemingxiang.entity.Teacher;
/**
* 执行增加
*/
@WebServlet("/add.do")
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");
//获取out
PrintWriter out = response.getWriter();
//接收值
String sid = request.getParameter("sid");
String sname = request.getParameter("sname");
//System.out.println(sid+sname+"======================");
String tid = request.getParameter("tname");
String cid = request.getParameter("cname");
String [] id = request.getParameterValues("hname");
String hb = "";
for (String string : id) {
hb+=string;
}
//System.out.println(hb+"=============="+id);
//调用biz层
ITeacBiz itb = new TeaBiz();
Teacher tea = itb.getTea(Integer.valueOf(tid));
IClassBiz icb = new ClassBiz();
Class clz = icb.getClz(Integer.valueOf(cid));
IStuBiz stb = new StuBiz();
Student stu = new Student(Integer.parseInt(sid), sname, clz, tea,hb);
int n = stb.addStu(stu);
//判断
if(n>0) {
out.print("");
}
else {
System.out.println("检查dao方法");
}
}
}
package com.hemingxiang.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.hemingxiang.biz.ClassBiz;
import com.hemingxiang.biz.HobbyBiz;
import com.hemingxiang.biz.IClassBiz;
import com.hemingxiang.biz.IHobbyBiz;
import com.hemingxiang.biz.IStuBiz;
import com.hemingxiang.biz.ITeacBiz;
import com.hemingxiang.biz.StuBiz;
import com.hemingxiang.biz.TeaBiz;
import com.hemingxiang.entity.Class;
import com.hemingxiang.entity.Habby;
import com.hemingxiang.entity.Student;
import com.hemingxiang.entity.Teacher;
/**
* 修改前的修改界面的绑值
*/
@WebServlet("/preupdate.do")
public class PreUpdateServlet 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");
//调用biz层
IStuBiz stb = new StuBiz();
Student stu = stb.getStu(Integer.parseInt(sid));
IHobbyBiz ihb = new HobbyBiz();
IClassBiz icb = new ClassBiz();
List clzall = icb.getAll();
ITeacBiz itb = new TeaBiz();
List teaall = itb.getAll();
List hall = ihb.getAll();
//实例化一个爱好的集合
List ls = new ArrayList();
String ss = stu.getSs();
String[] split = ss.split(",");
for (String string : split) {
Habby hob = ihb.getHob(Integer.parseInt(string));
//加进去
ls.add(hob);
}
//将集合存起来
request.setAttribute("clzall", clzall);
request.setAttribute("teaall", teaall);
request.setAttribute("ls", ls);
request.setAttribute("hall", hall);
request.setAttribute("stu", stu);
//+clzall+teaall+ls+hall
//System.out.println(stu+"=================");
//转发
request.getRequestDispatcher("update.jsp").forward(request, response);
}
}
package com.hemingxiang.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.hemingxiang.biz.ClassBiz;
import com.hemingxiang.biz.IClassBiz;
import com.hemingxiang.biz.IStuBiz;
import com.hemingxiang.biz.ITeacBiz;
import com.hemingxiang.biz.StuBiz;
import com.hemingxiang.biz.TeaBiz;
import com.hemingxiang.entity.Class;
import com.hemingxiang.entity.Student;
import com.hemingxiang.entity.Teacher;
/**
* 执行修改操作
*/
@WebServlet("/update.do")
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");
//获取out
PrintWriter out = response.getWriter();
//接收值
String sid = request.getParameter("sid");
String sname = request.getParameter("sname");
String tid = request.getParameter("tname");
String cid = request.getParameter("cname");
String [] id = request.getParameterValues("hname");
String hb = "";
for (String string : id) {
hb+=string;
}
//调用biz层
ITeacBiz itb = new TeaBiz();
Teacher tea = itb.getTea(Integer.valueOf(tid));
IClassBiz icb = new ClassBiz();
Class clz = icb.getClz(Integer.valueOf(cid));
IStuBiz stb = new StuBiz();
Student stu = new Student(Integer.parseInt(sid), sname, clz, tea, hb);
int n = stb.upStu(stu, Integer.parseInt(sid));
//判断
if(n>0) {
out.print("");
}
else {
System.out.println("检查dao方法");
}
}
}
package com.hemingxiang.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.hemingxiang.biz.ClassBiz;
import com.hemingxiang.biz.IClassBiz;
import com.hemingxiang.biz.IStuBiz;
import com.hemingxiang.biz.ITeacBiz;
import com.hemingxiang.biz.StuBiz;
import com.hemingxiang.biz.TeaBiz;
import com.hemingxiang.entity.Class;
import com.hemingxiang.entity.Student;
import com.hemingxiang.entity.Teacher;
/**
* 执行删除
*/
@WebServlet("/delete.do")
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");
//System.out.println(sid+"===================");
//调用biz层
IStuBiz stb = new StuBiz();
int n = stb.delStu(Integer.parseInt(sid));
//判断
if(n>0) {
out.print("");
}
else {
System.out.println("检查dao方法");
}
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
Insert title here
主界面
学生的id
学生的姓名
学生的教员
学生的所在班级
学生的爱好
操作
${stu.sid}
${stu.sname}
${stu.tea.tname}
${stu.c.cname}
${h.hname}
修改
删除
首页
上一页
[${pageIndex}/${max}]
下一页
尾页
界面
删除功能
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
Insert title here
新增页面
界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
Insert title here
修改页面
界面