选课系统的dao层
package Dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import java.sql.ResultSet; import java.sql.SQLException; import DBUtil.DBUtil; import information.*; public class Dao { public boolean tadd(information in) { String sql = "insert into text2(tid,tname,tsex,tlocation,tzc) values('"+ in.getTid() + "','"+ in.getTname() + "','" + in.getTsex() + "','" + in.getTlocation() + "','" + in.getTzc() +"')"; System.out.println(sql); Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 1; try { state = conn.createStatement(); state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); a=0; } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } public boolean sadd(student s) { String sql = "insert into text3(sid,sname,ssex,sclass,szy) values('"+ s.getSid() + "','"+ s.getSname() + "','"+s.getSsex() + "','" + s.getSclass() + "','" + s.getSzy() +"')"; Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 1; try { state = conn.createStatement(); state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); a=0; } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } public boolean classadd(kecheng in) { String sql = "insert into class(kid,kname,knumber,kteacher) values('"+ in.getKid()+ "','"+ in.getKname() + "','" + in.getKnumber() + "','" + in.getKteacher() +"')"; System.out.println(sql); Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 1; try { state = conn.createStatement(); state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); a=0; } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } public List<information> tcha (String m) { String sql="select * from text2 where "; if(m!="") { sql+="tname = '"+m+"'"; } System.out.println(sql); Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; List<information> list = new ArrayList<>(); try { state = conn.createStatement(); rs = state.executeQuery(sql); information bean = null; while (rs.next()) { String tid = rs.getString("tid"); String tname = rs.getString("tname"); String tsex = rs.getString("tsex"); String tlocation=rs.getString("tlocation"); String tzc = rs.getString("tzc"); System.out.println(tid); bean=new information(tid,tname,tsex,tlocation,tzc); list.add(bean); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } return list; } public List<student> scha (String m) { String sql="select * from text3 where "; if(m!="") { sql+="sname = '"+m+"'"; } Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; List<student> list = new ArrayList<>(); try { state = conn.createStatement(); rs = state.executeQuery(sql); student bean = null; while (rs.next()) { String sid = rs.getString("sid"); String sname = rs.getString("sname"); String ssex = rs.getString("ssex"); String sclass=rs.getString("sclass"); String szy = rs.getString("szy"); bean=new student(sid,sname,ssex,sclass,szy); list.add(bean); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } return list; } public boolean tsan(String x) { String sql="delete from text2 where tname='"+x+"'"; Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 1; try { state = conn.createStatement(); state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); a=0; } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } public boolean ssan(String x) { String sql="delete from text3 where sname='"+x+"'"; Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 1; try { state = conn.createStatement(); state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); a=0; } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } }
package Servlet; import java.util.List; import java.io.IOException; 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 Dao.Dao; import information.*; @WebServlet("/Servlet") public class Servlet extends HttpServlet { private static final long serialVersionUID = 1L; public Servlet() { super(); } protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if ("tadd".equals(method)) { tadd(req, resp); } if ("sadd".equals(method)) { sadd(req, resp); } if ("classadd".equals(method)) { classadd(req, resp); } if ("tjudge".equals(method)) { tjudge(req, resp); } if ("sjudge".equals(method)) { sjudge(req, resp); } if ("cha".equals(method)) { cha(req, resp); } if ("tgai".equals(method)) { tgai(req, resp); } if ("sgai".equals(method)) { sgai(req, resp); } if ("save".equals(method)) { save(req, resp); } if ("ssave".equals(method)) { ssave(req, resp); } } private void tadd(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); HttpSession session = req.getSession(); String tid = req.getParameter("tid"); String tname = req.getParameter("tname"); String tsex = req.getParameter("tsex"); String tlocation = req.getParameter("tlocation"); String tzc = req.getParameter("tzc"); session.setAttribute("tid",tid); session.setAttribute("tname",tname); session.setAttribute("tsex",tsex); session.setAttribute("tlocation",tlocation); session.setAttribute("tzc",tzc); System.out.println(tid); System.out.println(tname); System.out.println(tsex); System.out.println(tlocation); System.out.println(tzc); information information = new information(tid,tname,tsex,tlocation,tzc); Dao dao =new Dao(); boolean f=dao.tadd(information); System.out.println(f); if(f) { req.setAttribute("message", "添加成功!"); req.getRequestDispatcher("face.jsp").forward(req,resp); } else { req.setAttribute("message", "添加失败!"); req.getRequestDispatcher("tadd.jsp").forward(req,resp); } } private void sadd(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String sid = req.getParameter("sid"); String sname = req.getParameter("sname"); String ssex = req.getParameter("ssex"); String sclass = req.getParameter("sclass"); String szy = req.getParameter("szy"); student student=new student(sid,sname,ssex,sclass,szy); Dao dao =new Dao(); boolean f=dao.sadd(student); System.out.println(f); if(f) { req.setAttribute("message", "添加成功!"); req.getRequestDispatcher("face.jsp").forward(req,resp); } else { req.setAttribute("message", "添加失败!"); req.getRequestDispatcher("sadd.jsp").forward(req,resp); } } private void classadd(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); HttpSession session = req.getSession(); String kid = req.getParameter("kid"); String kname = req.getParameter("kname"); String knumber= req.getParameter("knumber"); String kteacher = (String) session.getAttribute("tname"); System.out.println(kid); kecheng kecheng = new kecheng(kid,kname,knumber,kteacher); Dao dao =new Dao(); boolean f=dao.classadd(kecheng); System.out.println(f); if(f) { req.setAttribute("message", "添加成功!"); req.getRequestDispatcher("face.jsp").forward(req,resp); } else { req.setAttribute("message", "添加失败!"); req.getRequestDispatcher("teacher.jsp").forward(req,resp); } } private void tjudge(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); HttpSession session = req.getSession(); String tid = req.getParameter("tid"); String tname = req.getParameter("tname"); session.setAttribute("tid",tid); session.setAttribute("tname",tname); Dao dao=new Dao(); List<information> holds = dao.tcha((String) session.getAttribute("tname")); req.setAttribute("holds", holds); req.getRequestDispatcher("teacher.jsp").forward(req,resp); } private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); HttpSession session = req.getSession(); Dao dao=new Dao(); List<information> holds = dao.tcha((String) session.getAttribute("tname")); req.setAttribute("holds", holds); req.getRequestDispatcher("tmodify.jsp").forward(req,resp); } private void ssave(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); HttpSession session = req.getSession(); Dao dao=new Dao(); List<student> holds = dao.scha((String) session.getAttribute("sname")); req.setAttribute("holds", holds); req.getRequestDispatcher("smodify.jsp").forward(req,resp); } private void sjudge(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); HttpSession session = req.getSession(); String sid = req.getParameter("sid"); String sname = req.getParameter("sname"); Dao dao=new Dao(); session.setAttribute("sid",sid); session.setAttribute("sname",sname); System.out.println(sname); List<student> holds = dao.scha(sname); req.setAttribute("holds", holds); req.getRequestDispatcher("student.jsp").forward(req,resp); } private void cha(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); HttpSession session = req.getSession(); Dao dao=new Dao(); String k=(String) session.getAttribute("tname"); System.out.println(k); List<information> holds = dao.tcha(k); req.setAttribute("holds", holds); req.getRequestDispatcher("teacher.jsp").forward(req,resp); } private void tgai(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String tid = req.getParameter("tid"); String tname = req.getParameter("tname"); String tsex = req.getParameter("tsex"); String tlocation = req.getParameter("tlocation"); String tzc = req.getParameter("tzc"); System.out.println(tid); System.out.println(tname); System.out.println(tsex); System.out.println(tlocation); System.out.println(tzc); information information = new information(tid,tname,tsex,tlocation,tzc); Dao dao =new Dao(); dao.tsan(tname); dao.tadd(information); req.getRequestDispatcher("teacher.jsp").forward(req,resp); } private void sgai(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String sid = req.getParameter("sid"); String sname = req.getParameter("sname"); String ssex = req.getParameter("ssex"); String sclass = req.getParameter("sclass"); String szy = req.getParameter("szy"); student student=new student(sid,sname,ssex,sclass,szy); Dao dao =new Dao(); HttpSession session = req.getSession(); String k=(String) session.getAttribute("sname"); dao.ssan(k); dao.sadd(student); req.getRequestDispatcher("student.jsp").forward(req,resp); } }
这里是servlet层