课程添加
建立各包
Bean层
package bean; public class Course { int id; private String teachername; private String place; private String classname; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTeachername() { return teachername; } public void setTeachername(String teachername) { this.teachername = teachername; } public String getPlace() { return place; } public void setPlace(String place) { this.place = place; } public String getClassname() { return classname; } public void setClassname(String classname) { this.classname = classname; } public Course(int id,String teachername,String place,String classname) { this.id=id; this.teachername=teachername; this.place=place; this.classname=classname; } public Course(String teachername,String place,String classname) { this.teachername=teachername; this.place=place; this.classname=classname; } }
dao层
package dao; import bean.Course; import java.beans.Statement; import java.sql.Connection; import java.sql.ResultSet; import java.util.ArrayList; import java.util.List; import DBUtil.DBUtil; public class Coursedao { public void courseadd(Course course) { String sql = "insert into runoob_db3(teachername,place,classname) values('" + course.getTeachername()+"','"+course.getPlace() + "','" +course.getClassname()+ "')"; //创建数据库链接 Connection conn = DBUtil.getConn(); java.sql.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); } } public List<Course> findByteachername(String teachername) { String sql = "select * from runoob_db3 where teachername ='" + teachername + "'"; List<Course> list = new ArrayList<>(); Connection conn = DBUtil.getConn(); java.sql.Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); Course bean = null; while (rs.next()) { int id=rs.getInt("id"); String place=rs.getString("place"); String classname = rs.getString("classname"); bean =new Course(id,teachername,place,classname); list.add(bean); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } }
DBUtil
package DBUtil; 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/runoob?serverTimezone=UTC"; public static String db_user = "root"; public static String db_pass = "152486"; public static Connection getConn() { Connection conn = null; try { Class.forName("com.mysql.cj.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(); } } } }
servlet层
package servlet; import java.util.Date; import java.text.SimpleDateFormat; 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 bean.Course; import dao.Coursedao; @WebServlet("/CourseServlet") public class CourseServlet extends HttpServlet{ private static final long serialVersionUID = 1L; Coursedao dao=new Coursedao(); protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if ("courseadd".equals(method)) { courseadd(req, resp); } else if("findByteachername".equals(method)) { findByteachername(req,resp); } //else if("chaxun".equals(method)) { // chaxun(req,resp); // }else if("liulan".equals(method)) { // liulan(req,resp); // }else if("delete2".equals(method)) { // delete2(req,resp); // }else if("getByname".equals(method)) { // getByname(req,resp); // }else if("getByname1".equals(method)) { // getByname1(req,resp); // }else if("xiugai".equals(method)) { // xiugai(req,resp); // } } private void courseadd(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); //获取数据 String teachername=req.getParameter("teachername"); String place=req.getParameter("place"); String classname=req.getParameter("classname"); Course course=new Course(teachername,place,classname); dao.courseadd(course); req.setAttribute("message", "录入成功"); req.getRequestDispatcher("add.jsp").forward(req,resp); } private void findByteachername(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); //获取数据 String teachername=req.getParameter("teachername"); List<Course> courses=dao.findByteachername(teachername); if(courses == null) { req.setAttribute("message", "查无此信息!"); req.getRequestDispatcher("find.jsp").forward(req,resp); } else { req.setAttribute("courses", courses); req.getRequestDispatcher("list.jsp").forward(req,resp); } } }
增加页面
package servlet; import java.util.Date; import java.text.SimpleDateFormat; 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 bean.Course; import dao.Coursedao; @WebServlet("/CourseServlet") public class CourseServlet extends HttpServlet{ private static final long serialVersionUID = 1L; Coursedao dao=new Coursedao(); protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getParameter("method"); if ("courseadd".equals(method)) { courseadd(req, resp); } else if("findByteachername".equals(method)) { findByteachername(req,resp); } //else if("chaxun".equals(method)) { // chaxun(req,resp); // }else if("liulan".equals(method)) { // liulan(req,resp); // }else if("delete2".equals(method)) { // delete2(req,resp); // }else if("getByname".equals(method)) { // getByname(req,resp); // }else if("getByname1".equals(method)) { // getByname1(req,resp); // }else if("xiugai".equals(method)) { // xiugai(req,resp); // } } private void courseadd(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); //获取数据 String teachername=req.getParameter("teachername"); String place=req.getParameter("place"); String classname=req.getParameter("classname"); Course course=new Course(teachername,place,classname); dao.courseadd(course); req.setAttribute("message", "录入成功"); req.getRequestDispatcher("add.jsp").forward(req,resp); } private void findByteachername(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); //获取数据 String teachername=req.getParameter("teachername"); List<Course> courses=dao.findByteachername(teachername); if(courses == null) { req.setAttribute("message", "查无此信息!"); req.getRequestDispatcher("find.jsp").forward(req,resp); } else { req.setAttribute("courses", courses); req.getRequestDispatcher("list.jsp").forward(req,resp); } } }