Student的增删改查
目录
一、底层代码
1.DBHelper
2.实体类
3.数据库访问层 dao
4.业务逻辑层
5.控制层
二、界面
1.主界面
2.增加界面
3.修改界面
4.查看界面
三、功能展示
绑值、
分页
模糊查询
增加
查看
删除
修改
一、底层代码
1.DBHelper
代码
#mysql driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8&useSSL=false user=root pwd=123456
方法
package com.heminjie.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 void close(Connection conn, Statement stmt, ResultSet rs,PreparedStatement ps) {close(rs);close(stmt);close(conn);close(ps);}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("数据库连接(关闭)成功");} }
2.实体类
班级类
package com.heminjie.entity; /*** 班级类* @author Administrator**/ public class Class {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;}@Overridepublic String toString() {return "Class [cid=" + cid + ", cname=" + cname + "]";}}
爱好类
package com.heminjie.entity;/*** 爱好类* @author Administrator**/ public class Hobby {private int hid;private String hname;public int getHid() {return hid;}public void setHid(int hid) {this.hid = hid;}public String getHname() {return hname;}public void setHname(String hname) {this.hname = hname;}public Hobby() {// TODO Auto-generated constructor stub}public Hobby(int hid, String hname) {this.hid = hid;this.hname = hname;}@Overridepublic String toString() {return "Hobby [hid=" + hid + ", hname=" + hname + "]";}}
学生类
package com.heminjie.entity;import java.io.Serializable; import java.util.List; /*** 学生类* @author Administrator**/ public class Student implements Serializable{private static final long serialVersionUID = 1L;private int sid;private String sname;private Class cl;private Theaher th;private List<Hobby> hy; // private String hys; // public String getHys() { // return hys; // } // public void setHys(String hys) { // this.hys = hys; // }public int getSid() {return sid;}public void setSid(int sid) {this.sid = sid;}public String getSname() {return sname;}public void setSname(String sname) {this.sname = sname;}public Class getCl() {return cl;}public void setCl(Class cl) {this.cl = cl;}public Theaher getTh() {return th;}public void setTh(Theaher th) {this.th = th;}public List<Hobby> getHy() {return hy;}public void setHy(List<Hobby> hy) {this.hy = hy;}public Student() {// TODO Auto-generated constructor stub}public Student(int sid, String sname, Class cl, Theaher th, List<Hobby> hy) {this.sid = sid;this.sname = sname;this.cl = cl;this.th = th;this.hy = hy;}public Student(String sname, Class cl, Theaher th, List<Hobby> hy) {this.sname = sname;this.cl = cl;this.th = th;this.hy = hy;} // public Student(String sname, Class cl, Theaher th, List<Hobby> hy,String hys) { // this.sname = sname; // this.cl = cl; // this.th = th; // this.hy = hy; // this.hys = hys; // }@Overridepublic String toString() {return "Student [sid=" + sid + ", sname=" + sname + ", cl=" + cl + ", th=" + th + ", hy=" + hy + "]";}}
教员类
package com.heminjie.entity; /*** 教员类* @author Administrator**/ public class Theaher {private int tid;private String tname;private Class cid;public int getTid() {return tid;}public void setTid(int tid) {this.tid = tid;}public String getTname() {return tname;}public void setTname(String tname) {this.tname = tname;}public Class getCid() {return cid;}public void setCid(Class cid) {this.cid = cid;}public Theaher() {// TODO Auto-generated constructor stub}public Theaher(int tid, String tname, Class cid) {this.tid = tid;this.tname = tname;this.cid = cid;}@Overridepublic String toString() {return "Theaher [tid=" + tid + ", tname=" + tname + ", cid=" + cid + "]";}}
3.数据库访问层 dao
班级类(接口)
package com.heminjie.dao;import java.util.List;import com.heminjie.entity.Class;public interface IClDao {/*** 爱好集合* @return*/public List<Class> getAll();/*** 查询单个* @param cid 班级编号* @return 返回对象*/public Class getdg(int cid);}
学生类(接口)
package com.heminjie.dao;import java.util.List;import com.heminjie.entity.Class; import com.heminjie.entity.Hobby; import com.heminjie.entity.Student; import com.heminjie.entity.Theaher;public interface IStuDao {/*** 学生集合* @return*/public List<Student> getAll(String cid,String tid,String hid,int pageIndex,int pageSize);/*** 查询单个学生* @param sid 学生编号* @return 返回单个学生*/public Student getdg(int sid);/*** 增加* @param stu* @return*/public int addStu(String sname, Class cl, Theaher th, List<Hobby> hy);/*** 删除* @param sid 删除id* @return 返回行数*/public int delStu(int sid);/*** 修改* @param sid 学生id* @param sname 学生姓名* @param cl 班级* @param th 教员* @param hy 爱好* @return 返回执行行数*/public int updStu(int sid,String sname, Class cl, Theaher th, List<Hobby> hy);/*** 总数* @return*/public int count(String cid,String tid,String hid);}
爱好类(接口)
package com.heminjie.dao;import java.util.List;import com.heminjie.entity.Hobby;public interface IHyDao {/*** 爱好集合* @return*/public List<Hobby> getAll();/*** 爱好查询单个* @param hid 爱好编号* @return 返回爱好对象*/public Hobby getdg(int hid); }
教员类(接口)
package com.heminjie.dao;import java.util.List;import com.heminjie.entity.Theaher;public interface IThDao {/*** 教员集合* @return*/public List<Theaher> getAll();/*** 查看单个* @param tid* @return*/public Theaher getdg(int tid);}
班级类(方法)
package com.heminjie.dao;import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List;import com.heminjie.entity.Class; import com.heminjie.entity.Hobby; import com.heminjie.util.DBHelper;public class ClDao implements IClDao{//扩大作用域Connection conn = null;Statement stmt = null;ResultSet rs = null;@Overridepublic List<Class> getAll() {List<Class> ls = new ArrayList<Class>();try {//连接数据库conn = DBHelper.getConnection();//定义sql语句String sql = "select * from h_bj";//执行sql语句stmt = conn.prepareStatement(sql);//获得结果集rs = stmt.executeQuery(sql);//遍历结果集while(rs.next()) {//实例化Class cl = new Class(rs.getInt(1),rs.getString(2));//加到集合中ls.add(cl);}} catch (Exception e) {e.printStackTrace();} finally {DBHelper.close(conn, stmt, rs);}return ls;}@Overridepublic Class getdg(int cid) {Class cl = new Class();try {//连接数据库conn = DBHelper.getConnection();//定义sql语句String sql = "select * from h_bj where cid="+cid+"";//执行sql语句stmt = conn.prepareStatement(sql);//获得结果集rs = stmt.executeQuery(sql);//遍历结果集while(rs.next()) {//实例化cl = new Class(rs.getInt(1),rs.getString(2));}} catch (Exception e) {e.printStackTrace();} finally {DBHelper.close(conn, stmt, rs);}return cl;}public static void main(String[] args) {System.out.println(new ClDao().getdg(1));}}
学生类(方法)
package com.heminjie.dao;import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import java.sql.Statement;import com.heminjie.biz.HyBiz; import com.heminjie.biz.IHyBiz; import com.heminjie.entity.Class; import com.heminjie.entity.Hobby; import com.heminjie.entity.Student; import com.heminjie.entity.Theaher; import com.heminjie.util.DBHelper;public class StuDao implements IStuDao{//扩大作用域Connection conn = null;Statement stmt = null;ResultSet rs = null;PreparedStatement ps = null;//爱好IHyBiz ihb = new HyBiz();@Overridepublic List<Student> getAll(String cid,String tid,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.hname,c.cname,d.tname from h_xs a,h_ah b,h_bj c,h_ls d where a.cid=c.cid and a.tid=d.tid and a.hid = b.hid\r\n" + ")q "+cid+"\r\n" + ")w "+tid+"\r\n" + ")e "+hid+"\r\n" + ")r limit "+a+","+pageSize+""; // System.out.println(sql);//执行sql语句stmt = conn.prepareStatement(sql);//获得结果集rs = stmt.executeQuery(sql);//遍历结果及while(rs.next()) {List<Hobby> hy = new ArrayList<>();String str = rs.getString("hid");String[] split = str.split(" ");for (String hids : split) {//查询单个Hobby hby = ihb.getdg(Integer.parseInt(hids));hy.add(hby);}//班级Class cl = new Class(rs.getInt("cid"),rs.getString("cname"));//教员Theaher th = new Theaher(rs.getInt("tid"),rs.getString("tname"),cl);//学生Student stu = new Student(rs.getInt("sid"),rs.getString("sname"),cl,th,hy);//加到集合中ls.add(stu);}} catch (Exception e) {e.printStackTrace();} finally {DBHelper.close(conn, stmt, rs);}return ls;}@Overridepublic Student getdg(int sid) {Student stu = new Student();try {//连接数据库conn = DBHelper.getConnection();//定义sql语句String sql = "select * from (\r\n" + "select a.*,b.hname,c.cname,d.tname from h_xs a,h_ah b,h_bj c,h_ls d where a.cid=c.cname and a.tid=d.tid and a.hid = b.hid ) e where e.sid="+sid+"";//执行sql语句stmt = conn.prepareStatement(sql);//获得结果集rs = stmt.executeQuery(sql);//遍历结果及while(rs.next()) {List<Hobby> hy = new ArrayList<>();String str = rs.getString("hid");String[] split = str.split(" ");for (String hid : split) {//查询单个Hobby hby = ihb.getdg(Integer.parseInt(hid));hy.add(hby);}//班级Class cl = new Class(rs.getInt("cid"),rs.getString("cname"));//教员Theaher th = new Theaher(rs.getInt("tid"),rs.getString("tname"),cl);//学生stu = new Student(rs.getInt("sid"),rs.getString("sname"),cl,th,hy);}} catch (Exception e) {e.printStackTrace();} finally {DBHelper.close(conn, stmt, rs);}return stu;}@Overridepublic int addStu(String sname, Class cl, Theaher th, List<Hobby> hy) {String hh = "";for (int i = 0; i < hy.size(); i++) {hh+=hy.get(i).getHid()+" ";}int n = 0;try {//连接数据库conn = DBHelper.getConnection();//定义sql语句String sql = "insert into h_xs(sname,cid,tid,hid) values('"+sname+"',"+cl.getCid()+","+th.getTid()+",'"+hh+"')";//执行sql语句stmt = conn.prepareStatement(sql);//返回执行的行数n = stmt.executeUpdate(sql);} catch (Exception e) {e.printStackTrace();} finally {DBHelper.close(conn, stmt, rs,ps);}return n;}@Overridepublic int delStu(int sid) {int n = 0;try {//连接数据库conn = DBHelper.getConnection();//定义sql语句String sql = "delete from h_xs where sid="+sid+"";//执行sql语句stmt = conn.prepareStatement(sql);//返回执行的行数n = stmt.executeUpdate(sql);} catch (Exception e) {e.printStackTrace();} finally {DBHelper.close(conn, stmt, rs, ps);}return n;}@Overridepublic int updStu(int sid, String sname, Class cl, Theaher th, List<Hobby> hy) {int n = 0;String hh = "";for (int i = 0; i < hy.size(); i++) {hh+=hy.get(i).getHid()+" ";}try {//连接数据库conn = DBHelper.getConnection();//定义sql语句String sql = "update h_xs set sname='"+sname+"',cid="+cl.getCid()+",tid="+th.getTid()+",hid='"+hh+"' where sid="+sid+"";//执行sql语句stmt = conn.prepareStatement(sql);//返回执行的行数n = stmt.executeUpdate(sql);} catch (Exception e) {e.printStackTrace();} finally {DBHelper.close(conn, stmt, rs, ps);}return n;}@Overridepublic 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.hname,c.cname,d.tname from h_xs a,h_ah b,h_bj c,h_ls d where a.cid=c.cid and a.tid=d.tid and a.hid = b.hid\r\n" + ")q "+cid+" \r\n" + ")w "+tid+" \r\n" + ")e "+hid+" \r\n" + ")t"; // System.out.println(sql);//执行sql语句stmt = conn.prepareStatement(sql);//结果集rs = stmt.executeQuery(sql);if(rs.next()) {//返回执行的行数n = rs.getInt(1);}} catch (Exception e) {e.printStackTrace();} finally {DBHelper.close(conn, stmt, rs, ps);;}return n;}//public static void main(String[] args) { // System.out.println(new StuDao().count()); //}// public static void main(String[] args) { // Class cl = new Class(1,"da"); // Theaher th = new Theaher(12,"fa",cl); // Hobby hsy1 = new Hobby(1,"sa"); // Hobby hsy2 = new Hobby(2,"se"); // Hobby hsy3 = new Hobby(3,"sf"); // List<Hobby> hy = new ArrayList<>(); // hy.add(hsy1); // hy.add(hsy2); // hy.add(hsy3); // // System.out.println(new StuDao().updStu(3,"juan", cl, th, hy)); // }}
爱好类(方法)
package com.heminjie.dao;import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List;import com.heminjie.entity.Hobby; import com.heminjie.util.DBHelper;public class HyDao implements IHyDao{//扩大作用域Connection conn = null;Statement stmt = null;ResultSet rs = null;@Overridepublic List<Hobby> getAll() {List<Hobby> ls = new ArrayList<Hobby>();try {//连接数据库conn = DBHelper.getConnection();//定义sql语句String sql = "select * from h_ah";//执行sql语句stmt = conn.prepareStatement(sql);//获得结果集rs = stmt.executeQuery(sql);//遍历结果集while(rs.next()) {//实例化Hobby hy = new Hobby(rs.getInt(1),rs.getString(2));//加到集合中ls.add(hy);}} catch (Exception e) {e.printStackTrace();} finally {DBHelper.close(conn, stmt, rs);}return ls;}@Overridepublic Hobby getdg(int hid) {Hobby hy = new Hobby();try {//连接数据库conn = DBHelper.getConnection();//定义sql语句String sql = "select * from h_ah where hid="+hid+"";//执行sql语句stmt = conn.prepareStatement(sql);//获得结果集rs = stmt.executeQuery(sql);//遍历结果集while(rs.next()) {//实例化hy = new Hobby(rs.getInt(1),rs.getString(2));}} catch (Exception e) {e.printStackTrace();} finally {DBHelper.close(conn, stmt, rs);}return hy;}}
教员类(方法)
package com.heminjie.dao;import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List;import com.heminjie.entity.Class; import com.heminjie.entity.Theaher; import com.heminjie.util.DBHelper;public class ThDao implements IThDao{//扩大作用域Connection conn = null;Statement stmt = null;ResultSet rs = null;@Overridepublic List<Theaher> getAll() {List<Theaher> ls = new ArrayList<Theaher>();try {//连接数据库conn = DBHelper.getConnection();//定义sql语句String sql = "select * from h_ls";//执行sql语句stmt = conn.prepareStatement(sql);//获得结果集rs = stmt.executeQuery(sql);//遍历结果集while(rs.next()) {//实例化班级Class cl = new Class(rs.getInt(1),rs.getString(2));//实例化Theaher th = new Theaher(rs.getInt(1),rs.getString(2),cl);//加到集合中ls.add(th);}} catch (Exception e) {e.printStackTrace();} finally {DBHelper.close(conn, stmt, rs);}return ls;}@Overridepublic Theaher getdg(int tid) {//教师Theaher th = new Theaher();try {//连接数据库conn = DBHelper.getConnection();//定义sql语句String sql = "select * from (\r\n" + "select t.*,c.cname from h_ls t,h_bj c where t.cid = c.cid\r\n" + ")a where a.tid = "+tid+"";//执行sql语句stmt = conn.prepareStatement(sql);//获得结果集rs = stmt.executeQuery(sql);//遍历结果集while(rs.next()) {//实例化班级Class cl = new Class(rs.getInt("cid"),rs.getString("cname"));//实例化th = new Theaher(rs.getInt("tid"),rs.getString("tname"),cl);}} catch (Exception e) {e.printStackTrace();} finally {DBHelper.close(conn, stmt, rs);}return th;}// public static void main(String[] args) { // System.out.println(new ThDao().getdg(5)); // }}
4.业务逻辑层
接口
学生biz
package com.heminjie.biz;import java.util.List;import com.heminjie.entity.Class;
import com.heminjie.entity.Hobby;
import com.heminjie.entity.Student;
import com.heminjie.entity.Theaher;public interface IStuBiz {/*** 学生集合* @return*/public List<Student> getAll(String cid,String tid,String hid,int pageIndex,int pageSize);/*** 查询单个学生* @param sid 学生编号* @return 返回单个学生*/public Student getdg(int sid);/*** 增加* @param stu* @return*/public int addStu(String sname, Class cl, Theaher th, List<Hobby> hy);/*** 删除* @param sid 删除id* @return 返回行数*/public int delStu(int sid);/*** 修改* @param sid 学生id* @param sname 学生姓名* @param cl 班级* @param th 教员* @param hy 爱好* @return 返回执行行数*/public int updStu(int sid,String sname, Class cl, Theaher th, List<Hobby> hy);/*** 总数* @return*/public int count(String cid,String tid,String hid);}
爱好biz
package com.heminjie.biz;import java.util.List;import com.heminjie.entity.Hobby;public interface IHyBiz {/*** 爱好集合* @return*/public List<Hobby> getAll();/*** 爱好查询单个* @param hid 爱好编号* @return 返回爱好对象*/public Hobby getdg(int hid);
}
班级biz
package com.heminjie.biz;import java.util.List;import com.heminjie.entity.Class;public interface IClBiz {/*** 爱好集合* @return*/public List<Class> getAll();/*** 查询单个* @param cid 班级编号* @return 返回对象*/public Class getdg(int cid);}
教员biz
package com.heminjie.biz;import java.util.List;import com.heminjie.entity.Theaher;public interface IThBiz {/*** 教员集合* @return*/public List<Theaher> getAll();/*** 查看单个* @param tid* @return*/public Theaher getdg(int tid);}
5.控制层
servlet
主页面绑值(模糊查询、分页)
package com.heminjie.servlet;import java.io.IOException;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.heminjie.biz.ClBiz;
import com.heminjie.biz.HyBiz;
import com.heminjie.biz.IClBiz;
import com.heminjie.biz.IHyBiz;
import com.heminjie.biz.IStuBiz;
import com.heminjie.biz.IThBiz;
import com.heminjie.biz.StuBiz;
import com.heminjie.biz.ThBiz;
import com.heminjie.entity.Class;
import com.heminjie.entity.Hobby;
import com.heminjie.entity.Student;
import com.heminjie.entity.Theaher;/*** 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 = 4;//接收表单String cid = request.getParameter("cid");if(cid==null) {cid="";}String tid= request.getParameter("tid");if(tid==null) {tid="";}String[] hids = request.getParameterValues("hid");String hid = "";String hidd ="";//用来拼接if(hids==null) {hidd=" ";hid="";}else {for (String str : hids) {hidd+=str+" ";//用空格进行隔开hid= " where hid like '%"+hidd+"%' ";}}String pid = request.getParameter("pid");if(pid!=null){//说明点了x页pageIndex = Integer.parseInt(pid);}String gid = request.getParameter("gid");
// System.out.println("ska:"+gid);if(gid==null) {gid="";}else if(gid=="") {gid="";}else{pageIndex = Integer.parseInt(gid);}//业务逻辑层IStuBiz isb = new StuBiz();IHyBiz ihb = new HyBiz();//爱好IThBiz itb = new ThBiz();//教员IClBiz icb = new ClBiz();//班级//调用查询全部的方法List<Student> stu = isb.getAll(cid, tid,hid, pageIndex, pageSize);List<Hobby> hobby12 = ihb.getAll();List<Theaher> theaher12 = itb.getAll();List<Class> clas12 = icb.getAll();int count = isb.count(cid, tid, hid);int pagecount=0;if(count%pageSize==1) {pagecount=count/pageSize+1;}else {pagecount=count/pageSize;}if(stu.size()!=0) {//加到集合中request.setAttribute("stu", stu);request.setAttribute("hobby12", hobby12);request.setAttribute("theaher12", theaher12);request.setAttribute("clas12", clas12);request.setAttribute("count", count);request.setAttribute("pagecount", pagecount);request.setAttribute("pageIndex", pageIndex);//转发request.getRequestDispatcher("index.jsp").forward(request, response);}else {System.out.println("集合为空");}}}
2.增加界面前的:
package com.heminjie.servlet;import java.io.IOException;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.heminjie.biz.ClBiz;
import com.heminjie.biz.HyBiz;
import com.heminjie.biz.IClBiz;
import com.heminjie.biz.IHyBiz;
import com.heminjie.biz.IThBiz;
import com.heminjie.biz.ThBiz;
import com.heminjie.entity.Class;
import com.heminjie.entity.Hobby;
import com.heminjie.entity.Theaher;/*** Servlet implementation class FoundServlet*/
@WebServlet("/FoundServlet")
public class FoundServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//设置编码方式request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");response.setContentType("text/html; charset=UTF-8");//业务逻辑层IHyBiz ihb = new HyBiz();//爱好IThBiz itb = new ThBiz();//教员IClBiz icb = new ClBiz();//班级//调用查询全部的方法List<Hobby> hobby = ihb.getAll();List<Theaher> theaher = itb.getAll();List<Class> clas = icb.getAll();//判断if(hobby!=null&&theaher!=null&&clas!=null) {//加到集合中request.setAttribute("hobby", hobby);request.setAttribute("theaher", theaher);request.setAttribute("clas", clas);//转发request.getRequestDispatcher("add.jsp").forward(request, response);}else {System.out.println("集合为空");}}}
3.增加功能:
package com.heminjie.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.heminjie.biz.ClBiz;
import com.heminjie.biz.HyBiz;
import com.heminjie.biz.IClBiz;
import com.heminjie.biz.IHyBiz;
import com.heminjie.biz.IStuBiz;
import com.heminjie.biz.IThBiz;
import com.heminjie.biz.StuBiz;
import com.heminjie.biz.ThBiz;
import com.heminjie.entity.Class;
import com.heminjie.entity.Hobby;
import com.heminjie.entity.Student;
import com.heminjie.entity.Theaher;/*** 增加* @author Administrator**/
@WebServlet("/AddServlet")
public class AddServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//设置编码方式request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");response.setContentType("text/html; charset=UTF-8");//获取表单提交过来的值String sname = request.getParameter("sname");String tid = request.getParameter("tid");String cid = request.getParameter("cid");String [] hids = request.getParameterValues("hid");String hsy = "";//用来拼接for (String str : hids) {hsy+=str+" ";//用空格进行隔开}PrintWriter out = response.getWriter();//业务逻辑层IThBiz itb = new ThBiz();IClBiz icb = new ClBiz();IHyBiz ihb = new HyBiz();IStuBiz isb = new StuBiz();List<Hobby> hy = new ArrayList<>();String[] split = hsy.split(" ");for (String hid : split) {//查询单个Hobby hby = ihb.getdg(Integer.parseInt(hid));hy.add(hby);}//教员查询单个Theaher th = itb.getdg(Integer.parseInt(tid));//班级查询单个Class cl = icb.getdg(Integer.parseInt(cid));//调用增加的方法int n = isb.addStu(sname, cl, th, hy); if(n>0) {out.print("<script>alert('增加成功');location.href='index.jsp'</script>");}else {out.print("<script>alert('增加失败');location.href='index.jsp'</script>");}}}
4.查看功能:
package com.heminjie.servlet;import java.io.IOException;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.heminjie.biz.ClBiz;
import com.heminjie.biz.HyBiz;
import com.heminjie.biz.IClBiz;
import com.heminjie.biz.IHyBiz;
import com.heminjie.biz.IStuBiz;
import com.heminjie.biz.IThBiz;
import com.heminjie.biz.StuBiz;
import com.heminjie.biz.ThBiz;
import com.heminjie.entity.Class;
import com.heminjie.entity.Hobby;
import com.heminjie.entity.Student;
import com.heminjie.entity.Theaher;/*** Servlet implementation class CheckServlet*/
@WebServlet("/CheckServlet")
public class CheckServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doPost(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//设置编码方式request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");response.setContentType("text/html; charset=UTF-8");//接收表单提交过来的值String sid = request.getParameter("sid");//业务逻辑层IHyBiz ihb = new HyBiz();//爱好IThBiz itb = new ThBiz();//教员IClBiz icb = new ClBiz();//班级IStuBiz isb = new StuBiz();//学生//调用查询全部的方法List<Hobby> hobby2 = ihb.getAll();List<Theaher> theaher2 = itb.getAll();List<Class> clas2 = icb.getAll();Student student2 = isb.getdg(Integer.parseInt(sid));List<Hobby> hy = student2.getHy();String bb = "";for (Hobby hobby : hy) {int hid = hobby.getHid();bb+=hid+" ";}
// Student student2 = isb.getdg(2);//判断if(hobby2!=null&&theaher2!=null&&clas2!=null&&student2!=null) {//加到集合中request.setAttribute("hobby2", hobby2);request.setAttribute("theaher2", theaher2);request.setAttribute("clas2", clas2);request.setAttribute("student2", student2);request.setAttribute("bb", bb);//转发request.getRequestDispatcher("check.jsp").forward(request, response);}else {System.out.println("集合为空");}}}
5.修改前功能:
package com.heminjie.servlet;import java.io.IOException;
import java.util.ArrayList;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import com.heminjie.biz.ClBiz;
import com.heminjie.biz.HyBiz;
import com.heminjie.biz.IClBiz;
import com.heminjie.biz.IHyBiz;
import com.heminjie.biz.IStuBiz;
import com.heminjie.biz.IThBiz;
import com.heminjie.biz.StuBiz;
import com.heminjie.biz.ThBiz;
import com.heminjie.entity.Class;
import com.heminjie.entity.Hobby;
import com.heminjie.entity.Student;
import com.heminjie.entity.Theaher;/*** 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");//业务逻辑层IHyBiz ihb = new HyBiz();//爱好IThBiz itb = new ThBiz();//教员IClBiz icb = new ClBiz();//班级IStuBiz isb = new StuBiz();//学生//调用查询全部的方法List<Hobby> hobby1 = ihb.getAll();List<Theaher> theaher1 = itb.getAll();List<Class> clas1 = icb.getAll();Student student1 = isb.getdg(Integer.parseInt(sid));
// Student student1 = isb.getdg(3);List<Hobby> hy = student1.getHy();String aa = "";for (Hobby hobby : hy) {int hid = hobby.getHid();aa+=hid+" ";}
// System.out.println(aa);
// Student student1 = isb.getdg(2);//判断if(hobby1!=null&&theaher1!=null&&clas1!=null&&student1!=null) {//加到集合中request.setAttribute("hobby1", hobby1);request.setAttribute("theaher1", theaher1);request.setAttribute("clas1", clas1);request.setAttribute("student1", student1);request.setAttribute("aa", aa);//转发request.getRequestDispatcher("update.jsp").forward(request, response);}else {System.out.println("集合为空");}}}
6.修改功能:
package com.heminjie.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.heminjie.biz.ClBiz;
import com.heminjie.biz.HyBiz;
import com.heminjie.biz.IClBiz;
import com.heminjie.biz.IHyBiz;
import com.heminjie.biz.IStuBiz;
import com.heminjie.biz.IThBiz;
import com.heminjie.biz.StuBiz;
import com.heminjie.biz.ThBiz;
import com.heminjie.entity.Class;
import com.heminjie.entity.Hobby;
import com.heminjie.entity.Theaher;/*** 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();//业务逻辑层IThBiz itb = new ThBiz();IClBiz icb = new ClBiz();IHyBiz ihb = new HyBiz();IStuBiz isb = new StuBiz();List<Hobby> hy = new ArrayList<>();String[] split = hsy.split(" ");for (String hid : split) {//查询单个Hobby hby = ihb.getdg(Integer.parseInt(hid));hy.add(hby);}//教员查询单个Theaher th = itb.getdg(Integer.parseInt(tid));//班级查询单个Class cl = icb.getdg(Integer.parseInt(cid));//调用增加的方法int n = isb.updStu(Integer.parseInt(sid), sname, cl, th, hy);if(n>0) {out.print("<script>alert('修改成功');location.href='index.jsp'</script>");}else {out.print("<script>alert('修改失败');location.href='XgServlet?sid="+sid+"'</script>");}}}
二、界面
1.主界面
<%@ 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><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="tid"><option value="" >请选择</option><c:forEach items="${theaher12 }" var="t"><option value=" where tid='${t.tid }'" >${t.tname}</option></c:forEach></select>班级:<select name="cid"><option value="" >请选择</option><c:forEach items="${clas12 }" var="c"><option value=" where cid='${c.cid }'" >${c.cname}</option></c:forEach></select>爱好:<input type="checkbox" name="hid" value="1" >篮球<input type="checkbox" name="hid" value="2" >足球<input type="checkbox" name="hid" value="3" >唱歌<input type="checkbox" name="hid" value="4" >跳舞<input type="submit" value="查询"><input type="button" value="增加" onclick="add()"></td></tr><tr><td>学生编号</td><td>学生姓名</td><td>学生教员</td><td>所在班级</td><td>学生爱好</td><td>管理操作</td></tr><c:forEach items="${stu}" var="s"><tr><td>${s.sid }</td><td>${s.sname}</td><td>${s.th.tname}</td><td>${s.cl.cname}</td><td><c:forEach items="${s.hy}" var="x">${x.hname}</c:forEach></td> <td><a href="CheckServlet?sid=${s.sid }">查看</a><a href="XgServlet?sid=${s.sid }">修改</a><a onclick="return confirm('你确定要删除吗?')" href="DeleteServlet?sid=${s.sid }">删除</a></td></tr></c:forEach></table><div>第${pageIndex}页, 共${pagecount}页, 总记录${count}条<a href="IndexServlet?pid=1">首页</a><a href="IndexServlet?pid=${pageIndex>1?pageIndex-1:1}">上一页</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}">下一页</a><a href="IndexServlet?pid=${pagecount}">尾页</a></div></form></center><script type="text/javascript">function add() {location.href="FoundServlet";}function go(id){var aa = document.getElementById(id);var bb = aa.value;alert(bb);if(isNaN(bb)||bb<0){alert("请输入正确的数字");}else{location.href="IndexServlet?gid="+bb+"";} }</script></body>
</html>
2.增加界面
<%@ 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="AddServlet" method="post"><table><tr><td>学生姓名</td><td><input type="text" name="sname"></td></tr><tr><td>学生教员</td><td><select name="tid"><c:forEach items="${theaher }" var="t"><option value="${t.tid }">${t.tname }</option></c:forEach></select></td></tr><tr><td>学生班级</td><td><select name="cid"><c:forEach items="${clas }" var="c"><option value="${c.cid }">${c.cname}</option></c:forEach></select></td></tr><tr><td>学生爱好</td><td><c:forEach items="${hobby }" var="h"><input type="checkbox" name="hid" value="${h.hid }">${h.hname }</c:forEach> </td></tr></table><input type="submit" value="确定"><input type="reset" value="清空"></form></center>
</body>
</html>
3.修改界面
<%@ 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="UpdateServlet" method="post"><table><tr><td>学生编号</td><td><input type="text" name="sid" value="${student1.sid }" readonly="readonly"></td></tr><tr><td>学生姓名</td><td><input type="text" name="sname" value="${student1.sname }" ></td></tr><tr><td>学生教员</td><td><select name="tid"><c:forEach items="${theaher1 }" var="t"><option value="${t.tid }" <c:if test="${student1.th.tid==t.tid }">selected="selected"</c:if> >${t.tname }</option></c:forEach></select></td></tr><tr><td>学生班级</td><td><select name="cid"><c:forEach items="${clas1 }" var="c"><option value="${c.cid }" <c:if test="${student1.cl.cid==c.cid }">selected="selected"</c:if> >${c.cname}</option></c:forEach></select></td></tr><tr><td>学生爱好</td><td><input type="checkbox" name="hid" value="1" <c:if test="${aa.contains('1 ') }">checked</c:if>>篮球<input type="checkbox" name="hid" value="2" <c:if test="${aa.contains('2 ') }">checked</c:if>>足球<input type="checkbox" name="hid" value="3" <c:if test="${aa.contains('3 ') }">checked</c:if>>唱歌<input type="checkbox" name="hid" value="4" <c:if test="${aa.contains('4 ') }">checked</c:if>>跳舞</td><%-- <td><c:forEach items="${hobby1 }" var="h"><input type="checkbox" name="hid" value="${h.hid }" <c:if test="${aa.contains('h.hid ')}">checked</c:if>>${h.hname }</c:forEach> </td> --%></tr></table><input type="submit" value="修改"><input type="reset" value="清空"></form></center>
</body>
</html>
4.查看界面
<%@ 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.sid }" readonly="readonly"></td></tr><tr><td>学生姓名</td><td><input type="text" name="sname" value="${student2.sname }" readonly="readonly"></td></tr><tr><td>学生教员</td><td><select name="tid"><c:forEach items="${theaher2 }" var="t"><option value="${t.tid }" <c:if test="${student2.th.tid==t.tid }">selected="selected"</c:if> disabled="disabled">${t.tname }</option></c:forEach></select></td></tr><tr><td>学生班级</td><td><select name="cid"><c:forEach items="${clas2 }" var="c"><option value="${c.cid }" <c:if test="${student2.cl.cid==c.cid }">selected="selected"</c:if> disabled="disabled">${c.cname}</option></c:forEach></select></td></tr><tr><td>学生爱好</td><td><input type="checkbox" name="hid" value="1" <c:if test="${bb.contains('1 ') }">checked</c:if>>篮球<input type="checkbox" name="hid" value="2" <c:if test="${bb.contains('2 ') }">checked</c:if>>足球<input type="checkbox" name="hid" value="3" <c:if test="${bb.contains('3 ') }">checked</c:if>>唱歌<input type="checkbox" name="hid" value="4" <c:if test="${bb.contains('4 ') }">checked</c:if>>跳舞</td><%-- <td><c:forEach items="${hobby2 }" var="h"><input type="checkbox" name="hid" value="${h.hid }" <c:if test="${h.hid.contains('h.hid ')}">checked</c:if> disabled="disabled">${h.hname }</c:forEach> </td> --%></tr></table><a href="index.jsp">返回</a></form></center>
</body>
</html>
三、功能展示
绑值、
分页
模糊查询
增加
查看
删除
修改
Student的增删改查相关推荐
- 02-CoreData 的增删改查
CoreData 的增删改查 基本的增删改查的操作 1 数据库的创建 - (void)createDB {// 1.1 创建路径NSURL *modelURL = [[NSBundle mainBun ...
- Entity Framework应用:根据实体的EntityState状态实现增删改查
在上一篇文章中,我们讲解了使用EF实现简单的增删改成,在这篇文章中我们使用实体的EntityState状态来优化数据的增删改查. 一.修改数据 上篇文章中的修改数据的方法是EF官方推荐的方式,即先查询 ...
- Python Web实战:Python+Django+MySQL实现基于Web版的增删改查
本文使用Python Web框架Django连接和操作MySQL数据库学生信息管理系统(SMS),主要包含对学生信息增删改查功能. 1.创建项目(sms) 创建Django项目 django-admi ...
- PyCharm Python3操作数据库MySQL增删改查
核心内容 (1)使用到的模块:PyMySQL 安装: pip3 install pymysql (2)连接数据库 # 连接数据库,地址:localhost,账号:root,密码:root,数据库:sc ...
- MVC、JSP实现mysql的增删改查功能的封装和简陋的界面交互
1.眼见为实 (1)欢迎界面,总索引:带下划线的三个都是链接 : (2)搜索界面:--有颜色的为链接 (3).点击上面图片中的修改链接之后就会弹出下面的修改界面: (4).修改用户信息的界面 (5). ...
- shell编程系列22--shell操作数据库实战之shell脚本与MySQL数据库交互(增删改查)
shell编程系列22--shell操作数据库实战之shell脚本与MySQL数据库交互(增删改查)Shell脚本与MySQL数据库交互(增删改查)# 环境准备:安装mariadb 数据库 [root ...
- mongodb基本操作=增删改查
这两天总算清闲下来了,这里只介绍mongodb的一些基本增删改查,从大到小,从数据库到下面的集合表等,这里推荐一个mongodb的可视化工具Robo3T,在上面操作增删改查使用起来也简单 数据库相关的 ...
- asp.net初学习实现简单的增删改查功能
在学习中发现网页模板可以大大节约重复的页面代码,同时对于WebSite开发的程序在不同的页面直接调用已写好的代码既不方面,只是作为代码量不大的程序进行开发还是比较方便. get和post的区别get是 ...
- 通过JDBC进行简单的增删改查(二)
本章笔记更易理解和学习,也是我第一次初学的笔记. 1 package javastudy; 2 3 import java.sql.Connection; 4 import java.sql.Driv ...
最新文章
- mysql编写中文时变成问号解决方法
- 运行python-thrift的DEMO
- 多项式加法 java 链表_多项式加法,用单链表实现。
- webdriver 爬虫 java_java爬虫通过selenium+WebDriver遍历页面链接报错
- 653. Two Sum IV - Input is a BST
- JDK源码解析之 Java.lang.StringBuilder
- Maven必知的7个问题,你会吗?
- tsm如何备份oracle原理,征文:TSM备份Oracle脚本及策略说明
- 女程序员在GitHub提交PR更易被拒?开源世界“她”力量
- 计算机组成原理实验(logisim)
- 裁员,缩招,冻结HC,程序员如何应对?
- PCI e 转 PXI 远程 转接 套件 PCI e / PXI 远程控制 器 模块 板卡
- Archive of Our Own_为什么说for...of循环是JS中的宝石?
- STM32与SYN6288语音合成模块的使用
- Your Organization has Turned off Automatic Updates (转载)
- 呜啦啦啦~我胡汉三又回来了
- 最优控制问题matlab编程实例,matlab实现解决最优控制中的内点约束问题
- C#计算数组的算术平均数、几何平均数、调和平均数、平方平均数和中位数
- E+H浊度仪维修CUE22-A1A浊度分析仪维修概述
- 常见漏洞原理及修复方式