标签:final method search except post out prot odi creat
package com.hjf.dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.hjf.entity.Course; import com.hjf.util.DBUtil; /** * 课程Dao * Dao层操作数据 * @author Hu * */ public class CourseDao { /** * 添加 * @param course * @return */ public static boolean add(Course course) { String sql = "insert into bumeng(departmentid,department) values(‘" + course.getDepartmentid() + "‘,‘" + course.getDepartment() + "‘)"; //创建数据库链接 Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } finally { //关闭连接 DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } /** * 删除 * * @param id * @return */ public boolean delete (String id) { boolean f = false; String sql = "delete from bumeng where departmentid=‘" + id + "‘"; Connection conn = DBUtil.getConn(); Statement state = null; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } /** * 修改 * @param name * @param pass */ public boolean update(Course course) { String sql = "update bumeng set departmentid=‘" + course.getDepartmentid() + "‘, department=‘" + course.getDepartment() + "‘ where departmentid=‘" + course.getDepartmentid() + "‘"; Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } /** * 验证课程名称是否唯一 * true --- 不唯一 * @param name * @return */ public boolean name(String name) { boolean flag = false; String sql = "select department from bumeng where departmentid = ‘" + name+ "‘"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { flag = true; } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return flag; } /** * 通过ID得到课程信息 * @param id * @return */ public Course getCourseById(String number) { String sql = "select * from bumeng where departmentid =‘" + number + "‘"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; Course course = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String departmentid = rs.getString("departmentid"); String department = rs.getString("department"); course = new Course(departmentid,department); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return course; } /** * 通过name得到Course * @param name * @return */ public Course getCourseByName(String name) { String sql = "select * from bumeng where department =‘" + name + "‘"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; Course course = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String departmentid = rs.getString("departmentid"); String department = rs.getString("department"); course = new Course(departmentid,department); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return course; } /** * 查找 * @param name * @param teacher * @param classroom * @return */ public List<Course> search(String departmentid, String department) { String sql = "select * from bumeng where "; if (departmentid != "") { sql += "departmentid like ‘%" + departmentid + "%‘"; } if (department != "") { sql += "departmentid like ‘%" + department + "%‘"; } List<Course> list = new ArrayList<>(); Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); Course bean = null; while (rs.next()) { String number2 = rs.getString("departmentid"); String name2 = rs.getString("department"); bean = new Course(number2,name2); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } /** * 全部数据 * @param name * @param teacher * @param classroom * @return */ public List<Course> list() { String sql = "select * from bumeng"; List<Course> list = new ArrayList<>(); Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); Course bean = null; while (rs.next()) { String leibie2 = rs.getString("departmentid"); String biaoti2 = rs.getString("department"); bean = new Course( leibie2, biaoti2); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } } package com.hjf.entity; public class Course { private String departmentid; private String department; public String getDepartmentid() { return departmentid; } public void setDepartmentid(String departmentid) { this.departmentid = departmentid; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public Course() {} public Course(String departmentid, String department) { this.departmentid = departmentid; this.department = department; } } package com.hjf.service; import java.util.List; import com.hjf.dao.CourseDao; import com.hjf.entity.Course; /** * CourseService * 服务层 * @author Hu * */ public class CourseService { CourseDao cDao = new CourseDao(); /** * 添加 * @param course * @return */ public boolean add(Course course) { CourseDao.add(course); return true; } /** * 删除 */ public void del(String id) { cDao.delete(id); } /** * 修改 * @return */ public void update(Course course) { cDao.update(course); } /** * 通过ID得到一个Course * @return */ public Course getCourseById(String id) { return cDao.getCourseById(id); } /** * 通过Name得到一个Course * @return */ public Course getCourseByName(String biaoti) { return cDao.getCourseByName(biaoti); } /** * 查找 * @return */ public List<Course> search(String number, String name) { return cDao.search( number,name); } /** * 全部数据 * @return */ public List<Course> list() { return cDao.list(); } } package com.hjf.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.hjf.entity.Course; import com.hjf.service.CourseService; @WebServlet("/CourseServlet") public class CourseServlet extends HttpServlet { private static final long serialVersionUID = 1L; CourseService service = new CourseService(); /** * 方法选择 */ protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if ("adds".equals(method)) { add(req, resp); } else if ("del".equals(method)) { del(req, resp); } else if ("update".equals(method)) { update(req, resp); } else if ("search".equals(method)) { search(req, resp); } else if ("getcoursebyid".equals(method)) { getCourseById(req, resp); } else if ("getcoursebyname".equals(method)) { getCourseByName(req, resp); } else if ("list".equals(method)) { list(req, resp); } } /** * 添加 * @param req * @param resp * @throws IOException * @throws ServletException */ private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); //获取数据 String departmentid = req.getParameter("departmentid"); String department = req.getParameter("department"); Course course = new Course(departmentid,department); //添加后消息显示 if(service.add(course)) { req.setAttribute("message", "添加成功"); req.getRequestDispatcher("adds.jsp").forward(req,resp); } else { req.setAttribute("message", "部门名重复,请重新录入"); req.getRequestDispatcher("adds.jsp").forward(req,resp); } } /** * 全部 * @param req * @param resp * @throws ServletException */ private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); List<Course> courses = service.list(); req.setAttribute("courses", courses); req.getRequestDispatcher("list.jsp").forward(req,resp); } /** * 通过ID得到Course * @param req * @param resp * @throws ServletException */ private void getCourseById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String id = req.getParameter("departmentid"); Course course = service.getCourseByName(id); req.setAttribute("course", course); req.getRequestDispatcher("detail2.jsp").forward(req,resp); } /** * 通过名字查找 * 跳转至删除 * @param req * @param resp * @throws IOException * @throws ServletException */ private void getCourseByName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String name = req.getParameter("departmentid"); Course course = service.getCourseByName(name); if(course == null) { req.setAttribute("message", "查无此课程!"); req.getRequestDispatcher("del.jsp").forward(req,resp); } else { req.setAttribute("course", course); req.getRequestDispatcher("detail.jsp").forward(req,resp); } } /** * 删除 * @param req * @param resp * @throws IOException * @throws ServletException */ private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String id = req.getParameter("departmentid"); service.del(id); req.setAttribute("message", "删除成功!"); req.getRequestDispatcher("del.jsp").forward(req,resp); } /** * 修改 * @param req * @param resp * @throws IOException * @throws ServletException */ private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String leibie = req.getParameter("departmentid"); String biaoti = req.getParameter("department"); Course course = new Course(leibie,biaoti); service.update(course); req.setAttribute("message", "修改成功"); req.getRequestDispatcher("CourseServlet?method=list").forward(req,resp); } /** * 查找 * @param req * @param resp * @throws ServletException */ private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String number = req.getParameter("departmentid"); String name = req.getParameter("department"); List<Course> courses = service.search(number,name); req.setAttribute("courses", courses); req.getRequestDispatcher("searchlist.jsp").forward(req,resp); } } package com.hjf.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; /** * 数据库连接工具 * @author Hu * */ public class DBUtil { public static String db_url = "jdbc:mysql://localhost:3306/user"; public static String db_user = "root"; public static String db_pass = "root"; public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver");//加载驱动 conn = DriverManager.getConnection(db_url, db_user, db_pass); } catch (Exception e) { e.printStackTrace(); } return conn; } /** * 关闭连接 * @param state * @param conn */ public static void close (Statement state, Connection conn) { if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, Statement state, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void main(String[] args) throws SQLException { Connection conn = getConn(); PreparedStatement pstmt = null; ResultSet rs = null; String sql ="select * from course"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); if(rs.next()){ System.out.println("空"); }else{ System.out.println("不空"); } } } package com.servlet; 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 course.Course; import dao.userDao; import entry.user; /** * Servlet implementation class Servlet */ @WebServlet("/Servlet") public class Servlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public Servlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response) */ protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String id = req.getParameter("id"); String pwd = req.getParameter("pwd"); String number = req.getParameter("number"); String name = req.getParameter("name"); String sex = req.getParameter("sex"); String clas = req.getParameter("clas"); String dengji = req.getParameter("dengji"); Course course = new Course(id,pwd,dengji,name,number,sex,clas); userDao.add(course); req.getRequestDispatcher("chenggong.jsp").forward(req,resp); } } package course; public class Course { private String id; private String pwd; private String number; private String name; private String sex; private String clas; private String dengji; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getClas() { return clas; } public void setClas(String clas) { this.clas = clas; } public String getDengji() { return dengji; } public void setDengji(String dengji) { this.dengji = dengji; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public Course() {} public Course(String id,String pwd,String dengji,String name,String number,String sex,String clas) { this.id = id; this.pwd = pwd; this.name = name; this.number = number; this.clas = clas; this.sex = sex; this.dengji = dengji; } } package dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import course.Course; import entry.user; import util.DBUtil; public class userDao { public static boolean add(Course course) { String sql = "insert into jiben(id,pwd,role,name,birthday,sex,department) values(‘" + course.getId() + "‘,‘" + course.getPwd() + "‘,‘" + course.getDengji() + "‘,‘" + course.getName() + "‘,‘" + course.getNumber() + "‘,‘" + course.getSex() + "‘,‘" + course.getClas() + "‘)"; //创建数据库链接 Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } finally { //关闭连接 DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } public static String id(String id,String pwd,String dengji) { String f="0"; String sql = "select * from jiben where id = ‘" + id + "‘ and pwd = ‘"+pwd+"‘"; // Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); if (rs.next()) { f = rs.getString(3); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return f; } } package entry; public class user { private String id; private String password; private String dengji; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getDengji() { return dengji; } public void setDEngji(String dengji) { this.dengji = dengji; } public user(){} public user( String password) { this.password = password; }; public user(String id, String password,String dengji) { super(); this.id = id; this.password = password; this.dengji = dengji; } private String departmentid; private String department; public String getDepartmentid() { return departmentid; } public void setDepartmentid(String departmentid) { this.departmentid = departmentid; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public void Course() {} public void Course(String departmentid, String department) { this.departmentid = departmentid; this.department = department; } } package s; import java.util.List; import course.Course; import sdao.CourseDao; public class service { static CourseDao cDao = new CourseDao(); /** * 添加 * @param course * @return */ public static boolean add(Course course) { CourseDao.add(course); return true; } /** * 删除 */ public static void del(String id) { cDao.delete(id); } /** * 修改 * @return */ public static void update(Course course) { cDao.update(course); } /** * 通过ID得到一个Course * @return */ public static Course getCourseById(String id) { return cDao.getCourseById(id); } /** * 通过Name得到一个Course * @return */ public static Course getCourseByName(String biaoti) { return cDao.getCourseByName(biaoti); } /** * 查找 * @return */ public static List<Course> search(String id,String pwd,String dengji,String name,String number,String sex,String clas) { return cDao.search(id,pwd,dengji,name,number,sex,clas); } /** * 全部数据 * @return */ public static List<Course> list() { return cDao.list(); } } package sdao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import course.Course; import util.DBUtil; public class CourseDao { public static boolean add(Course course) { String sql = "insert into jiben(id,pwd,role,name,birthday,sex,department) values(‘" + course.getId() + "‘,‘" + course.getPwd() + "‘,‘" + course.getDengji() + "‘,‘" + course.getName() + "‘,‘" + course.getNumber() + "‘,‘" + course.getSex() + "‘,‘" + course.getClas() + "‘)"; //创建数据库链接 Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } finally { //关闭连接 DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } /** * 删除 * * @param id * @return */ public boolean delete (String id) { boolean f = false; String sql = "delete from jiben where id=‘" + id + "‘"; Connection conn = DBUtil.getConn(); Statement state = null; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } /** * 修改 * @param name * @param pass */ public boolean update(Course course) { String sql = "update jiben set id=‘" + course.getId() + "‘, pwd=‘" + course.getPwd()+"‘, role=‘" + course.getDengji()+"‘, name=‘" + course.getName()+"‘, birthday=‘" + course.getNumber()+"‘, sex=‘" + course.getSex()+"‘, department=‘" + course.getClas()+ "‘ where id=‘" + course.getId() + "‘"; Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } /** * 验证课程名称是否唯一 * true --- 不唯一 * @param name * @return */ public boolean name(String name) { boolean flag = false; String sql = "select name from jiben where id = ‘" + name+ "‘"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { flag = true; } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return flag; } /** * 通过ID得到课程信息 * @param id * @return */ public Course getCourseById(String number) { String sql = "select * from jiben where id =‘" + number + "‘"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; Course course = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String id = rs.getString("id"); String pwd = rs.getString("pwd"); String dengji = rs.getString("role"); String name = rs.getString("name"); String umber = rs.getString("birthday"); String sex = rs.getString("sex"); String clas = rs.getString("department"); course = new Course( id, pwd, dengji,name, umber,sex, clas); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return course; } /** * 通过name得到Course * @param name * @return */ public Course getCourseByName(String name) { String sql = "select * from jiben where name =‘" + name + "‘"; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; Course course = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String id = rs.getString("id"); String pwd = rs.getString("pwd"); String dengji = rs.getString("role"); String nme = rs.getString("name"); String umber = rs.getString("birthday"); String sex = rs.getString("sex"); String clas = rs.getString("department"); course = new Course( id, pwd, dengji,nme, umber,sex, clas); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return course; } /** * 查找 * @param name * @param teacher * @param classroom * @return */ public List<Course> search(String id,String pwd,String dengji,String name,String number,String sex,String clas) { String sql = "select * from jiben where "; if (id != "") { sql += "id like ‘%" + id + "%‘"; } if (pwd != "") { sql += "pwd like ‘%" + pwd + "%‘"; } if (dengji != "") { sql += "role like ‘%" + dengji + "%‘"; } if (name != "") { sql += "name like ‘%" + name + "%‘"; } if (number != "") { sql += "birthday like ‘%" + number + "%‘"; } if (sex != "") { sql += "sex like ‘%" + sex + "%‘"; } if (clas != "") { sql += "department like ‘%" + clas + "%‘"; } List<Course> list = new ArrayList<>(); Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); Course bean = null; while (rs.next()) { String id2 = rs.getString("id"); String pwd2 = rs.getString("pwd"); String dengji2 = rs.getString("role"); String nme2 = rs.getString("name"); String umber2 = rs.getString("birthday"); String sex2 = rs.getString("sex"); String clas2 = rs.getString("department"); bean = new Course( id2, pwd2, dengji2,nme2, umber2,sex2, clas2); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } /** * 全部数据 * @param name * @param teacher * @param classroom * @return */ public List<Course> list() { String sql = "select * from jiben"; List<Course> list = new ArrayList<>(); Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); Course bean = null; while (rs.next()) { String id2 = rs.getString("id"); String pwd2 = rs.getString("pwd"); String dengji2 = rs.getString("role"); String nme2 = rs.getString("name"); String umber2 = rs.getString("birthday"); String sex2 = rs.getString("sex"); String clas2 = rs.getString("department"); bean = new Course( id2, pwd2, dengji2,nme2, umber2,sex2, clas2); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } } package serv; 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 sdao.CourseDao; import course.Course; import s.service; /** * Servlet implementation class servlet1 */ @WebServlet("/servlet1") public class servlet1 extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public servlet1() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response) */ protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if ("add".equals(method)) { add(req, resp); } else if ("del".equals(method)) { del(req, resp); } else if ("update".equals(method)) { update(req, resp); } else if ("search".equals(method)) { search(req, resp); } else if ("getcoursebyid".equals(method)) { getCourseById(req, resp); } else if ("getcoursebyname".equals(method)) { getCourseByName(req, resp); } else if ("list".equals(method)) { list(req, resp); } } /** * 添加 * @param req * @param resp * @throws IOException * @throws ServletException */ private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); //获取数据 String id = req.getParameter("id"); String pwd = req.getParameter("pwd"); String dengji = req.getParameter("role"); String name = req.getParameter("name"); String number = req.getParameter("birthday"); String sex = req.getParameter("sex"); String clas = req.getParameter("department"); Course course = new Course( id, pwd, dengji,name, number, sex, clas) ; //添加后消息显示 if(service.add(course)) { req.setAttribute("message", "添加成功"); req.getRequestDispatcher("add.jsp").forward(req,resp); } else { req.setAttribute("message", "部门名重复,请重新录入"); req.getRequestDispatcher("add.jsp").forward(req,resp); } } /** * 全部 * @param req * @param resp * @throws ServletException */ private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); List<Course> courses = service.list(); req.setAttribute("courses", courses); req.getRequestDispatcher("lists.jsp").forward(req,resp); } /** * 通过ID得到Course * @param req * @param resp * @throws ServletException */ private void getCourseById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String id = req.getParameter("id"); Course course = service.getCourseById(id); req.setAttribute("course", course); req.getRequestDispatcher("detail2s.jsp").forward(req,resp); } /** * 通过名字查找 * 跳转至删除 * @param req * @param resp * @throws IOException * @throws ServletException */ private void getCourseByName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); Course course = service.getCourseByName(name); if(course == null) { req.setAttribute("message", "查无此课程!"); req.getRequestDispatcher("dels.jsp").forward(req,resp); } else { req.setAttribute("course", course); req.getRequestDispatcher("details.jsp").forward(req,resp); } } /** * 删除 * @param req * @param resp * @throws IOException * @throws ServletException */ private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String id = req.getParameter("id"); service.del(id); req.setAttribute("message", "删除成功!"); req.getRequestDispatcher("dels.jsp").forward(req,resp); } /** * 修改 * @param req * @param resp * @throws IOException * @throws ServletException */ private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String id = req.getParameter("id"); String pwd = req.getParameter("pwd"); String dengji = req.getParameter("role"); String name = req.getParameter("name"); String number = req.getParameter("birthday"); String sex = req.getParameter("sex"); String clas = req.getParameter("department"); Course course = new Course( id, pwd, dengji,name, number, sex, clas) ; service.update(course); req.setAttribute("message", "修改成功"); req.getRequestDispatcher("servlet1?method=lists").forward(req,resp); } /** * 查找 * @param req * @param resp * @throws ServletException */ private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String id = req.getParameter("id"); String pwd = req.getParameter("pwd"); String dengji = req.getParameter("role"); String name = req.getParameter("name"); String number = req.getParameter("birthday"); String sex = req.getParameter("sex"); String clas = req.getParameter("department"); List<Course> courses = service.search(id,pwd,dengji,name,number,sex,clas); req.setAttribute("courses", courses); req.getRequestDispatcher("searchlist.jsp").forward(req,resp); } } package servlet; 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 course.Course; import dao.userDao; import entry.user; /** * Servlet implementation class AddServlet */ @WebServlet("/AddServlet") public class AddServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); // String id = request.getParameter("id"); String password = request.getParameter("password"); String dengji = request.getParameter("role"); System.out.println(id); System.out.println(password); if(!userDao.id(id,password,dengji).equals("0")) { request.setAttribute("message", "登录成功"); if(userDao.id(id,password,dengji).equals("1")) request.getRequestDispatcher("chenggong.jsp").forward(request,response); else if(userDao.id(id,password,dengji).equals("2")) request.getRequestDispatcher("teacher.jsp").forward(request,response); else request.getRequestDispatcher("student.jsp").forward(request,response); } else { request.setAttribute("message", "登录失败"); request.getRequestDispatcher("houtai.jsp").forward(request,response); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ } package util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtil { public static String db_url = "jdbc:mysql://localhost:3306/user?useSSL=false"; public static String db_user = "root"; public static String db_pass = "root"; public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver");//鍔犺浇椹卞姩 conn = DriverManager.getConnection(db_url, db_user, db_pass); } catch (Exception e) { e.printStackTrace(); } return conn; } /*10鍏抽棴杩炴帴*/ public static void close (Statement state, Connection conn) { if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void close (ResultSet rs, Statement state, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (state != null) { try { state.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } public static void main(String[] args) throws SQLException { Connection conn = getConn(); PreparedStatement pstmt = null; ResultSet rs = null; String sql ="select * from user"; pstmt = conn.prepareStatement(sql); rs = pstmt.executeQuery(); if(rs.next()){ System.out.println("123"); }else{ System.out.println("456"); } } }
标签:final method search except post out prot odi creat
原文地址:https://www.cnblogs.com/520520520zl/p/12309284.html