首先导入我们需要的jar包依赖
导入我们需要的js
结构目录:
各包展示
DBHelpoer.java
package com.xlb.util;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
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);
}
/**
* 关闭资源
* @param con 连接
* @param ps 执行对象
* @param rs 结果集
*/
public static void myClose(Connection con,PreparedStatement ps,ResultSet rs) {
try {
if(rs!=null) {
rs.close();
}
if(ps!=null) {
ps.close();
}
if(con!=null&&!con.isClosed()) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
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("数据库连接(关闭)成功");
}
}
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_0613?useUnicode=true&characterEncoding=UTF-8&useSSL=false
user=root
pwd=123
com.xlb.servlet包
IndexServlet.java
package com.xlb.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 javax.servlet.http.HttpSession;
import javax.websocket.Session;
import com.xlb.biz.ClassBiz;
import com.xlb.biz.IClassBiz;
import com.xlb.biz.ILikeBiz;
import com.xlb.biz.IStuBiz;
import com.xlb.biz.ITeacharBiz;
import com.xlb.biz.LikeBiz;
import com.xlb.biz.StuBiz;
import com.xlb.biz.TeacharBiz;
import com.xlb.dao.IStuDao;
import com.xlb.entity.Like;
import com.xlb.entity.Student;
import com.xlb.entity.Teachar;
/**
* 主界面servlet
* @author 波哥
*
* 2022年6月18日 下午7:33:36
*/
@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");
//拿到out
PrintWriter out = response.getWriter();
//拿到session
HttpSession session = request.getSession();
//定义分页数
int pageInde=1;
int pageSize=2;
//拿到值
String str = request.getParameter("str");//老师
String strr = request.getParameter("strr");//班级
String[] s = request.getParameterValues("strrr");//爱好
if(str==null) {
str="";
}
if(strr==null) {
strr="";
}
// System.out.println("老师"+str+"===班级"+strr+"爱好==="+s);
String sss="";
if(s!=null) {
for (String string : s) {
sss+=string+",";
}
}
System.out.println(sss);
//调用方法
IStuBiz isb=new StuBiz();
//班级类
IClassBiz icb=new ClassBiz();
//调用查询全部方法
List<com.xlb.entity.Class> classls = icb.getAll();
//老师类
ITeacharBiz itb=new TeacharBiz();
List<Teachar> teacharls = itb.getAll();
//爱好类
ILikeBiz ilk=new LikeBiz();
List<Like> likels = ilk.getAll();
//接收pid
String pid=request.getParameter("pid");
if(pid!=null) {
pageInde=Integer.parseInt(pid);
}
//调用最大页码
int max = isb.getMax(str, strr, sss, pageSize);
if(max==0) {
max=1;
}
//学生集合
List<Student> studentls = isb.getAll(str, strr, sss , pageInde, pageSize);
session.setAttribute("classls", classls);
session.setAttribute("teacharls", teacharls);
session.setAttribute("likels", likels);
//存进去
request.setAttribute("studentls", studentls);
request.setAttribute("max", max);
request.setAttribute("pageIndex", pageInde);
//转发
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
TyServlet.java
package com.xlb.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.xlb.biz.ClassBiz;
import com.xlb.biz.IClassBiz;
import com.xlb.biz.ILikeBiz;
import com.xlb.biz.IStuBiz;
import com.xlb.biz.ITeacharBiz;
import com.xlb.biz.LikeBiz;
import com.xlb.biz.StuBiz;
import com.xlb.biz.TeacharBiz;
import com.xlb.entity.Student;
/**
* 增删改查servlet
* @author 波哥
*
* 2022年6月20日 下午2:36:27
*/
@WebServlet("/aa.do")
public class TyServlet 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();
//调用业务逻辑层
IStuBiz isb=new StuBiz();
IClassBiz icb=new ClassBiz();
ITeacharBiz itb=new TeacharBiz();
ILikeBiz ilb=new LikeBiz();
//接收aa
String ssss = request.getParameter("a");
int l=Integer.parseInt(ssss);
if(l==0) {
//增加
//拿到表单的值
String sname = request.getParameter("sname");
String str = request.getParameter("str");//老师
String strr = request.getParameter("strr");//班级
String[] s = request.getParameterValues("strrr");//爱好
String like="";
for (String string : s) {
like+=string+",";
}
//调用方法
int n = isb.addStudent(new Student(sname, itb.getDg(Integer.parseInt(str)), icb.getDg(Integer.parseInt(strr)), like));
if(n>0) {
//增加成功
out.print("");
}
else {
//增加失败
out.print("");
}
}
if(l==1) {
//删除
//拿到id
String sid = request.getParameter("sid");
//调用方法
int n = isb.deleteStudent(sid);
if(n>0) {
//删除成功
out.print("");
}
else {
//删除失败
out.print("");
}
}
if(l==2) {
//修改邦值
//拿到id
String sid = request.getParameter("sid");
Student ss = isb.getDg(sid);
//存进去
request.setAttribute("stu", ss);
//转发
request.getRequestDispatcher("update.jsp").forward(request, response);
}
if(l==3) {
//拿到值
String sid = request.getParameter("sid");
String sname = request.getParameter("sname");
String str = request.getParameter("str");//老师
String strr = request.getParameter("strr");//班级
String[] s = request.getParameterValues("strrr");//爱好
String like="";
for (String string : s) {
like+=string+",";
}
//调用修改方法
int n=isb.updateStudent(new Student(Integer.parseInt(sid), sname, itb.getDg(Integer.parseInt(str)), icb.getDg(Integer.parseInt(strr)), like));
if(n>0) {
//修改成功IndexServlet
out.print("");
}
else {
//删除失败
out.print("");
}
}
}
}
com.xlb.entity包
Class.java
package com.xlb.entity;
import java.io.Serializable;
/**
* 班级实体类
* @author 波哥
*
* 2022年6月14日 下午3:00:43
*/
public class Class implements Serializable{
/**
*
*/
private static final long serialVersionUID = 8851742647135346944L;
private int cid;//班级id
private String tname;//班级名
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public Class() {
// TODO Auto-generated constructor stub
}
public Class(int cid, String tname) {
super();
this.cid = cid;
this.tname = tname;
}
public Class(String tname) {
this.tname = tname;
}
@Override
public String toString() {
return "Class [cid=" + cid + ", tname=" + tname + "]";
}
}
Like.java
package com.xlb.entity;
/**
* 爱好实体类
* @author 波哥
*
* 2022年6月14日 下午3:46:54
*/
public class Like {
private int lid;//爱好实体类
private String lname;//爱好名
public int getLid() {
return lid;
}
public void setLid(int lid) {
this.lid = lid;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public Like() {
// TODO Auto-generated constructor stub
}
public Like(int lid, String lname) {
super();
this.lid = lid;
this.lname = lname;
}
public Like(String lname) {
this.lname = lname;
}
@Override
public String toString() {
return "Like [lid=" + lid + ", lname=" + lname + "]";
}
}
Student.java
package com.xlb.entity;
import java.util.List;
/**
* 学生实体类
* @author 波哥
*
* 2022年6月14日 下午2:57:16
*/
public class Student {
private int sid;//学生id
private String sname;//学生姓名
private Teachar t;//老师实体类
private Class c;//班级实体类
//private String likee;//爱好
private List<Like> ah;
private String like;//用来增加修改删除
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 Teachar getT() {
return t;
}
public void setT(Teachar t) {
this.t = t;
}
public Class getC() {
return c;
}
public void setC(Class c) {
this.c = c;
}
public List<Like> getAh() {
return ah;
}
public void setAh(List<Like> ah) {
this.ah = ah;
}
public Student() {
// TODO Auto-generated constructor stub
}
public Student(int sid, String sname, Teachar t, Class c, List<Like> ah) {
this.sid = sid;
this.sname = sname;
this.t = t;
this.c = c;
this.ah = ah;
}
public Student(String sname, Teachar t, Class c, List<Like> ah) {
this.sname = sname;
this.t = t;
this.c = c;
this.ah = ah;
}
public String getLike() {
return like;
}
public void setLike(String like) {
this.like = like;
}
public Student(String sname, Teachar t, Class c, String like) {
super();
this.sname = sname;
this.t = t;
this.c = c;
this.like = like;
}
@Override
public String toString() {
return "Student [sid=" + sid + ", sname=" + sname + ", t=" + t + ", c=" + c + ", ah=" + ah + ", like=" + like
+ "]";
}
public Student(int sid, String sname, Teachar t, Class c, String like) {
super();
this.sid = sid;
this.sname = sname;
this.t = t;
this.c = c;
this.like = like;
}
}
Teachar.java
package com.xlb.entity;
import java.io.Serializable;
/**
* 老师实体类
* @author 波哥
*
* 2022年6月14日 下午2:58:10
*/
public class Teachar implements Serializable{
/**
*
*/
private static final long serialVersionUID = 2041300652578515543L;
private int tid;//老师id
private String tname;//老师名
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 Teachar() {
// TODO Auto-generated constructor stub
}
public Teachar(int tid, String tname) {
super();
this.tid = tid;
this.tname = tname;
}
@Override
public String toString() {
return "Teachar [tid=" + tid + ", tname=" + tname + "]";
}
public Teachar(String tname) {
this.tname = tname;
}
}
com.xlb.dao
ClassDao.java
package com.xlb.dao;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.xlb.entity.Class;
/**
* 班级数据库访问层
* @author 波哥
*
* 2022年6月14日 下午10:18:26
*/
public class ClassDao implements IClassDao{
//调用通用方法层
TyDao td=new TyDao();
@Override
public Class getDg(int cid) {
Class c=new Class();
try {
Object[] obj= {cid};
//调用方法
ResultSet rs=td.executeQuery("select * from tb_class where cid=?", obj);
if(rs.next()){
c.setCid(rs.getInt(1));
c.setTname(rs.getString(2));
}
} catch (Exception e) {
e.printStackTrace();
}
return c;
}
/**
* 查询全部班级
*/
@Override
public List<Class> getAll() {
List<Class> ls=new ArrayList<Class>();
try {
ResultSet rs=td.executeQuery("select * from tb_class");
while(rs.next()) {
//实例化一个班级对象
Class c=new Class();
c.setCid(rs.getInt(1));
c.setTname(rs.getString(2));
ls.add(c);
}
} catch (Exception e) {
e.printStackTrace();
}
return ls;
}
}
IClassDao.java
package com.xlb.dao;
import java.util.List;
import com.xlb.entity.Class;
/**
* 班级表数据库访问层
* @author 波哥
*
* 2022年6月15日 上午8:57:25
*/
public interface IClassDao {
/**
* 根据id查找班级
* @param cid 班级id
* @return 班级对象
*/
Class getDg(int cid);
/**
* 查询全部班级
* @return 班级集合
*/
List<Class> getAll();
}
ILikeDao.java
package com.xlb.dao;
import java.util.List;
import com.xlb.entity.Like;
/**
* 爱好表数据库访问层
* @author 波哥
*
* 2022年6月15日 上午8:57:25
*/
public interface ILikeDao {
/**
* 根据id查找爱好
* @param cid 爱好id
* @return 爱好对象
*/
Like getDg(int lid);
/**
* 查询全部爱好
* @return 查询全部爱好
*/
List<Like> getAll();
}
IStuDao.java
**package com.xlb.dao;
import java.util.List;
import com.xlb.entity.Student;
/**
* 学生数据库访问层
* @author 波哥
*
* 2022年6月14日 下午4:24:27
*/
public interface IStuDao {
/**
* 带分页的模糊查询
* @param str 老师关键字
* @param strr 班级关键字
* @param strrr 爱好关键字
* @param pageIndex 页码
* @param pageSize 每页多少页
* @return
*/
List<Student> getAll(String str,String strr,String strrr,int pageIndex,int pageSize);
/**
* 增加学生方法
* @param stu 学生对象
* @return 影响行数
*/
int addStudent(Student stu);
/**
* 拿到最大页码
* @param str 老师关键字
* @param strr 班级关键字
* @param strrr 爱好关键字
* @return 最大页码
*/
int getMax(String str,String strr,String strrr);
/**
* 根据id删除学生
* @param sid 学生id
* @return 影响行数
*/
int deleteStudent(String sid);
/**
* 根据id查询单个
* @param sid 学生id
* @return 学生对象
*/
Student getDg(String sid);
/**
* 修改
* @param s 学生对象
* @return 影响行数
*/
int updateStudent(Student s);
}
**
ITeacharDao.java
package com.xlb.dao;
import java.util.List;
import com.xlb.entity.Like;
import com.xlb.entity.Teachar;
/**
* 通用数据库访问层
* @author 波哥
*
* 2022年6月14日 下午4:29:23
*/
public interface ITeacharDao {
/**
* 根据id查找单个老师
* @param tid 老师id
* @return 老师对象
*/
Teachar getDg(int tid);
/**
* 查询全部老师
* @return 老师对象
*/
List<Teachar> getAll();
}
LikeDao.java
package com.xlb.dao;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.xlb.entity.Class;
import com.xlb.entity.Like;
/**
* 爱好数据库访问层
* @author 波哥
*
* 2022年6月14日 下午10:18:26
*/
public class LikeDao implements ILikeDao{
//调用通用方法层
TyDao td=new TyDao();
@Override
public Like getDg(int lid) {
Like l=new Like();
try {
Object[] b= {lid};
ResultSet rs=td.executeQuery("select * from tb_like where lid=?", b);
if(rs.next()) {
l.setLid(rs.getInt(1));
l.setLname(rs.getString(2));
}
} catch (Exception e) {
e.printStackTrace();
}
return l;
}
/**
* 查询全部爱好
*/
@Override
public List<Like> getAll() {
List<Like> ls=new ArrayList<Like>();
try {
ResultSet rs=td.executeQuery("select * from tb_like");
while(rs.next()) {
//实例化一个爱好对象
Like c=new Like();
c.setLid(rs.getInt(1));
c.setLname(rs.getString(2));
ls.add(c);
}
} catch (Exception e) {
e.printStackTrace();
}
return ls;
}
}
StuDao.java
package com.xlb.dao;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.xlb.entity.Like;
import com.xlb.entity.Student;
import com.xlb.util.DBHelper;
/**
* 学生数据库访问层
* @author 波哥
*
* 2022年6月14日 下午10:24:21
*/
public class StuDao implements IStuDao{
//调用通用方法层
TyDao td=new TyDao();
//调用其他dao层
LikeDao ld=new LikeDao();
ClassDao cd=new ClassDao();
TeacharDao tdd =new TeacharDao();
@Override
public List<Student> getAll(String str,String strr,String strrr, int pageIndex, int pageSize) {
int a=(pageIndex-1)*pageSize;
List<Student> ls=new ArrayList<>();
try {
String sql="select * FROM tb_student WHERE tid like '%"+str+"%' and cid like '%"+strr+"%' and likee like '%"+strrr+"%' LIMIT ?,? ";
Object[] obj= {a,pageSize};
ResultSet rs=td.executeQuery(sql, obj);
while(rs.next()) {
//实例化一个学生
Student sd=new Student();
//实例化一个爱好数组
List<Like> ll=new ArrayList<Like>();
sd.setSid(rs.getInt(1));
sd.setSname(rs.getString(2));
sd.setT(tdd.getDg(rs.getInt(3)));
sd.setC(cd.getDg(rs.getInt(4)));
String s = rs.getString(5);//爱好字符串 利用逗号分割
String[] split = s.split(",");
for (String string : split) {
ll.add(ld.getDg(Integer.parseInt(string)));
}
//加到学生实体类
sd.setAh(ll);
//最后加到大集合里面
ls.add(sd);
}
} catch (Exception e) {
e.printStackTrace();
}
return ls;
}
/**
* 增加方法
*/
@Override
public int addStudent(Student stu) {
int n = 0;
try {
String sql="insert into tb_student(sname,tid,cid,likee) values(?,?,?,?)";
Object[] ob= {stu.getSname(),stu.getT().getTid(),stu.getC().getCid(),stu.getLike()};
n=td.tyzsc(sql,ob);
} catch (Exception e) {
e.printStackTrace();
}
return n;
}
/**
* 拿到最大行数
*/
@Override
public int getMax(String str, String strr, String strrr) {
int n=0;
try {
String sql="select count(*) from tb_student where tid like '%"+str+"%' and cid like '%"+strr+"%' and likee like '%"+strrr+"%' ";
ResultSet rs = td.executeQuery(sql);
if(rs.next()) {
n=rs.getInt(1);
}
} catch (Exception e) {
e.printStackTrace();
}
return n;
}
/**
* 删除学生
*/
@Override
public int deleteStudent(String sid) {
int n = 0;
try {
String sql="delete from tb_student where sid=?";
Object[] obj= {sid};
n=td.tyzsc(sql, obj);
} catch (Exception e) {
e.printStackTrace();
}
return n;
}
/**
* 根据id查询单个
*/
@Override
public Student getDg(String sid) {
Student stu=new Student();
try {
String sql="select * from tb_student where sid=?";
Object[] obj= {sid};
ResultSet rs=td.executeQuery(sql, obj);
if(rs.next()) {
List<Like> ls=new ArrayList<>();
stu.setSid(rs.getInt(1));
stu.setSname(rs.getString(2));
stu.setT(tdd.getDg(rs.getInt(3)));
stu.setC(cd.getDg(rs.getInt(4)));
String ss=rs.getString(5);
//用逗号分割
String[] ll = ss.split(",");
for (String kk : ll) {
//调用查询
Like dg = ld.getDg(Integer.parseInt(kk));
ls.add(dg);
}
stu.setAh(ls);
}
} catch (Exception e) {
e.printStackTrace();
}
return stu;
}
@Override
public int updateStudent(Student s) {
int n=0;
try {
String sql="update tb_student set sname=?,tid=?,cid=?,likee=? where sid=? ";
Object[] obj={s.getSname(),s.getT().getTid(),s.getC().getCid(),s.getLike(),s.getSid()};
n=td.tyzsc(sql, obj);
} catch (Exception e) {
e.printStackTrace();
}
return n;
}
}
TeacharDao.java
package com.xlb.dao;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.xlb.entity.Like;
import com.xlb.entity.Teachar;
/**
* 老师dao方法
* @author 波哥
*
* 2022年6月14日 下午4:32:45
*/
public class TeacharDao implements ITeacharDao{
//调用通用方法层
TyDao td=new TyDao();
private ResultSet rs=null;
/**
* 查看单个教师方法
*/
@Override
public Teachar getDg(int tid) {
Teachar t=new Teachar();
try {
String sql="select * from tb_teachar where tid=?";
Object[] obj= {tid};
rs=td.executeQuery(sql, obj);
if(rs.next()) {
t.setTid(rs.getInt(1));
t.setTname(rs.getString(2));
}
} catch (Exception e) {
e.printStackTrace();
}
return t;
}
/**
* 查询全部老师
*/
@Override
public List<Teachar> getAll() {
List<Teachar> ls=new ArrayList<Teachar>();
try {
ResultSet rs=td.executeQuery("select * from tb_teachar");
while(rs.next()) {
//实例化一个爱好对象
Teachar c=new Teachar();
c.setTid(rs.getInt(1));
c.setTname(rs.getString(2));
ls.add(c);
}
} catch (Exception e) {
e.printStackTrace();
}
return ls;
}
}
TyDao.java
package com.xlb.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;
import com.xlb.entity.Like;
import com.xlb.util.DBHelper;
/**
* 通用dao方法
* @author 波哥
*
* 2022年6月14日 下午3:49:29
*/
public class TyDao {
//三兄弟
private Connection con=null;
private PreparedStatement ps=null;
private ResultSet rs=null;
/**
* 通用增删改
* @param sql sql语句
* @param objects 占位符
* @return 影响行数
*/
public int tyzsc(String sql,Object...objects) {
int n = 0;
try {
con=DBHelper.getConnection();
ps=con.prepareStatement(sql);
css(ps, objects);
n=ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return n;
}
/**
* 通用叠层
* @param ps
* @param objects
*/
public void css(PreparedStatement ps,Object...objects) {
try {
if(objects!=null) {
for (int i = 0; i < objects.length; i++) {
ps.setObject(i+1, objects[i]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 通用查询方法
* @param sql sql语句
* @param objects 占位符
* @return
*/
public List<Object> select(String sql,Object...objects){
List<Object> ls=new ArrayList<Object>();
try {
con=DBHelper.getConnection();
ps=con.prepareStatement(sql);
//获取表元数据
//设置参数
css(ps, objects);
rs=ps.executeQuery();
ResultSetMetaData data = rs.getMetaData();
while(rs.next()) {
//data.getColumnCount() 列
for (int i = 1; i <= data.getColumnCount(); i++) {
ls.add(rs.getObject(i));
}
}
} catch (Exception e) {
e.printStackTrace();
}finally {
DBHelper.myClose(con, ps, rs);
}
return ls;
}
/**
* 方法功能:通用查询
*
* @param sql sql语句
* @param Object...objs
*/
public ResultSet executeQuery(String sql, Object... objects) {
try {
con = DBHelper.getConnection();
ps = con.prepareStatement(sql);
for (int i = 0; i < objects.length; i++) {
ps.setObject(i + 1, objects[i]);
}
rs = ps.executeQuery();
} catch (Exception e) {
System.out.println("DAO查询异常");
e.printStackTrace();
}
return rs;
}
}
com.xlb.biz
ClassBiz.java
package com.xlb.biz;
import java.util.List;
import com.xlb.dao.ClassDao;
import com.xlb.dao.IClassDao;
import com.xlb.dao.ITeacharDao;
import com.xlb.dao.TeacharDao;
import com.xlb.entity.Class;
/**
* 班级表业务逻辑层
* @author 波哥
*
* 2022年6月14日 下午10:15:07
*/
public class ClassBiz implements IClassBiz{
//调用数据库访问层
IClassDao icd=new ClassDao();
@Override
public Class getDg(int cid) {
return icd.getDg(cid);
}
@Override
public List<Class> getAll() {
// TODO Auto-generated method stub
return icd.getAll();
}
}
IClassBiz.java
package com.xlb.biz;
import java.util.List;
import com.xlb.entity.Class;
/**
* 班级表业务逻辑层
* @author 波哥
*
* 2022年6月15日 上午8:57:25
*/
public interface IClassBiz {
/**
* 根据id查找班级
* @param cid 班级id
* @return 班级对象
*/
Class getDg(int cid);
/**
* 查询全部班级
* @return 班级集合
*/
List<Class> getAll();
}
ILikeBiz.java
package com.xlb.biz;
import java.util.List;
import com.xlb.entity.Like;
/**
* 爱好表业务逻辑层层
* @author 波哥
*
* 2022年6月15日 上午8:57:25
*/
public interface ILikeBiz {
/**
* 根据id查找爱好
* @param cid 爱好id
* @return 爱好对象
*/
Like getDg(int lid);
/**
* 查询全部爱好
* @return 查询全部爱好
*/
List<Like> getAll();
}
IStuBiz.java
package com.xlb.biz;
import java.util.List;
import com.xlb.entity.Student;
/**
* 学生业务逻辑层
* @author 波哥
*
* 2022年6月15日 上午9:31:46
*/
public interface IStuBiz {
/**
* 带分页的模糊查询
* @param str 老师关键字
* @param strr 班级关键字
* @param strrr 爱好关键字
* @param pageIndex 页码
* @param pageSize 每页多少页
* @return
*/
List<Student> getAll(String str,String strr,String strrr,int pageIndex,int pageSize);
/**
* 增加学生方法
* @param stu 学生对象
* @return 影响行数
*/
int addStudent(Student stu);
/**
* 拿到最大页码
* @param str 老师关键字
* @param strr 班级关键字
* @param strrr 爱好关键字
* @return 最大页码
*/
int getMax(String str,String strr,String strrr,int pageSize);
/**
* 根据id删除学生
* @param sid 学生id
* @return 影响行数
*/
int deleteStudent(String sid);
/**
* 根据id查询单个
* @param sid 学生id
* @return 学生对象
*/
Student getDg(String sid);
/**
* 修改
* @param s 学生对象
* @return 影响行数
*/
int updateStudent(Student s);
}
ITeacharBiz.java
package com.xlb.biz;
import java.util.List;
import com.xlb.entity.Teachar;
/**
* 业务逻辑层
* @author 波哥
*
* 2022年6月14日 下午10:13:28
*/
public interface ITeacharBiz {
/**
* 根据id查找单个老师
* @param tid 老师id
* @return 老师对象
*/
Teachar getDg(int tid);
/**
* 查询全部老师
* @return 老师对象
*/
List<Teachar> getAll();
}
LikeBiz.java
package com.xlb.biz;
import java.util.List;
import com.xlb.dao.ILikeDao;
import com.xlb.dao.LikeDao;
import com.xlb.entity.Like;
/**
* 爱好表业务逻辑层
* @author 波哥
*
* 2022年6月14日 下午10:15:07
*/
public class LikeBiz implements ILikeBiz{
//调用数据库访问层
ILikeDao ikd=new LikeDao();
@Override
public Like getDg(int lid) {
return ikd.getDg(lid);
}
@Override
public List<Like> getAll() {
return ikd.getAll();
}
}
StuBiz.java
package com.xlb.biz;
import java.util.List;
import com.xlb.dao.IStuDao;
import com.xlb.dao.StuDao;
import com.xlb.entity.Student;
/**
* 学生数据库访问层
* @author 波哥
*
* 2022年6月15日 上午9:32:28
*/
public class StuBiz implements IStuBiz{
//调用数据访问层
IStuDao isd=new StuDao();
@Override
public List<Student> getAll(String str, String strr, String strrr, int pageIndex, int pageSize) {
return isd.getAll(str, strr, strrr, pageIndex, pageSize);
}
@Override
public int getMax(String str, String strr, String strrr,int pageSize) {
int rows=isd.getMax(str, strr, strrr);//拿到总行数
int max=rows/pageSize;//求页码
if(rows%pageSize!=0) {//判断是否能够除尽
max++;
}
return max;
}
@Override
public int addStudent(Student stu) {
// TODO Auto-generated method stub
return isd.addStudent(stu);
}
@Override
public int deleteStudent(String sid) {
// TODO Auto-generated method stub
return isd.deleteStudent(sid);
}
@Override
public Student getDg(String sid) {
// TODO Auto-generated method stub
return isd.getDg(sid);
}
@Override
public int updateStudent(Student s) {
// TODO Auto-generated method stub
return isd.updateStudent(s);
}
}
TeacharBiz.java
package com.xlb.biz;
import java.util.List;
import com.xlb.dao.ITeacharDao;
import com.xlb.dao.TeacharDao;
import com.xlb.entity.Teachar;
/**
* 老师表业务逻辑层
* @author 波哥
*
* 2022年6月14日 下午10:15:07
*/
public class TeacharBiz implements ITeacharBiz{
//调用数据库访问层
ITeacharDao itd=new TeacharDao();
@Override
public Teachar getDg(int tid) {
return itd.getDg(tid);
}
@Override
public List<Teachar> getAll() {
// TODO Auto-generated method stub
return itd.getAll();
}
}
增加界面
<%@ 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>
<h1>增加界面</h1>
<form action="aa.do?a=0" method="post">
<table border="1px">
<tr>
<td>
学生姓名:<input type="text" name="sname" />
</td>
</tr>
<tr>
<td>
学生教员:
<!-- 教员下拉框 -->
<select name="str">
<c:forEach items="${teacharls}" var="t">
<option value="${t.tid}">${t.tname}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td>
学生班级:
<!-- 班级下拉框 -->
<select name="strr">
<c:forEach items="${classls}" var="c">
<option value="${c.cid}">${c.tname}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td>
学生爱好:
<!-- 爱好复选框 -->
<c:forEach items="${likels}" var="l">
<input type="checkbox" name="strrr" value="${l.lid}">${l.lname}
</c:forEach>
</td>
</tr>
</table>
<br/>
<input type="submit" value="增加" />
</form>
</center>
</body>
</html>
主界面
<%@ 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>
<h1>增加界面</h1>
<form action="aa.do?a=0" method="post">
<table border="1px">
<tr>
<td>
学生姓名:<input type="text" name="sname" />
</td>
</tr>
<tr>
<td>
学生教员:
<!-- 教员下拉框 -->
<select name="str">
<c:forEach items="${teacharls}" var="t">
<option value="${t.tid}">${t.tname}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td>
学生班级:
<!-- 班级下拉框 -->
<select name="strr">
<c:forEach items="${classls}" var="c">
<option value="${c.cid}">${c.tname}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td>
学生爱好:
<!-- 爱好复选框 -->
<c:forEach items="${likels}" var="l">
<input type="checkbox" name="strrr" value="${l.lid}">${l.lname}
</c:forEach>
</td>
</tr>
</table>
<br/>
<input type="submit" value="增加" />
</form>
</center>
</body>
</html>
修改界面
<%@ 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>
<h1>修改界面</h1>
<form action="aa.do?a=3&sid=${stu.sid}" method="post">
<table border="1px">
<tr>
<td>
学生姓名:<input type="text" value="${stu.sname}" name="sname" />
</td>
</tr>
<tr>
<td>
学生教员:
<!-- 教员下拉框 -->
<select name="str">
<c:forEach items="${teacharls}" var="t">
<option <c:if test="${t.tid==stu.t.tid}">selected="selected"</c:if> value="${t.tid}">${t.tname}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td>
学生班级:
<!-- 班级下拉框 -->
<select name="strr">
<c:forEach items="${classls}" var="c">
<option <c:if test="${c.cid==stu.c.cid}">selected="selected"</c:if> value="${c.cid}">${c.tname}</option>
</c:forEach>
</select>
</td>
</tr>
<tr>
<td>
学生爱好:
<!-- 爱好复选框 -->
<c:forEach items="${likels}" var="l">
<input <c:forEach items="${stu.ah}" var="y"><c:if test="${l.lname==y.lname}">checked="checked"</c:if></c:forEach> type="checkbox" name="strrr" value="${l.lid}">${l.lname}
</c:forEach>
</td>
</tr>
</table>
<br/>
<input type="submit" value="修改" />
</form>
</center>
</body>
</html>