java web + mysql 的增删改查
一、下载安装数据库(因为我的电脑之前安装的MySQL不能用,所以这次换成了SQLyog。安装,还不算难,不过之前安装过MySQL的,一定要把相关的东西尤其是表格什么的删干净!!!)
下载网址:https://downloads.mysql.com/archives/community/
下载64msi(版本视情况而定)
教程:
1. 打开软件,点击Next安装Mysql
2. 选择Custom定制安装。
3. 选则安装路径,建议保持默认路径或者把C盘改为D盘。然后下一步,
4. 点击Install安装
5. 点击NextàFinish完成安装。
6. 等待各个软件安装完成,点击下一步,则显示配置界面,直接下一步即可,
7. 点击下一步,知道出现数据路径配置界面,选择数据库数据存储路径。
8. 点击下一步,知道出现语言配置界面,选择最后一项,下拉框选择utf-8。
9. 点击下一步,知道出现配置mysql的链接密码界面,把密码设置为root,点下一步,进行配置。
10. 安装完成,点击finish完成安装
安装mysql界面操作软件
1. 打开“SQLyog 12.0.8.0.zip”文件进行安装,打开后会选择语言,选择自己熟悉的,点击ok。
2. 之后安装默认选项下一步安装即可。直到安装完成。
3 .建表
①右击表——创建新表然后根据自己需要建立新表。
二、建立Java web 项目(前提是配置好tomcat环境,网上有详细教程,就不详细说了)
1. eclipse新建一个Dynamic Web Project
2.将连接mysql的驱动jar包(mysql-connector-java-5.1.7-bin.jar)copy到WEB-INF下的lib目录下(jar包可以在网上下载)
3.新建五个包,entity存放实体类(User),dao存放数据库操作类(UserDao),servlet存放控制类(addServlet,updateServlet,deleteServlet,listServlet),util存放开发帮助类service是算法实现(这里是数据库操作帮助类,封装了数据库连接部分代码,避免大量重复代码)
4. 新建类,java类放在对应包中,jsp页面放在WebContent目录下,替换web.xml中内容,结构如图
三、快乐地搬代码时间
1 //Course.java 2 package com.hjf.entity; 3 4 public class Course { 5 6 private int id; 7 private String name; 8 private String teacher; 9 private String classroom; 10 11 public int getId() { 12 return id; 13 } 14 public void setId(int id) { 15 this.id = id; 16 } 17 public String getName() { 18 return name; 19 } 20 public void setName(String name) { 21 this.name = name; 22 } 23 public String getTeacher() { 24 return teacher; 25 } 26 public void setTeacher(String teacher) { 27 this.teacher = teacher; 28 } 29 public String getClassroom() { 30 return classroom; 31 } 32 public void setClassroom(String classroom) { 33 this.classroom = classroom; 34 } 35 36 public Course() {} 37 38 public Course(int id, String name, String teacher, String classroom) { 39 this.id = id; 40 this.name = name; 41 this.teacher = teacher; 42 this.classroom = classroom; 43 } 44 45 public Course(String name, String teacher, String classroom) { 46 this.name = name; 47 this.teacher = teacher; 48 this.classroom = classroom; 49 } 50 }
1 //CourseDao.java 2 package com.hjf.dao; 3 4 import java.sql.Connection; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.sql.Statement; 8 import java.util.ArrayList; 9 import java.util.List; 10 11 import com.hjf.entity.Course; 12 import com.hjf.util.DBUtil; 13 14 /** 15 * 课程Dao 16 * Dao层操作数据 17 * @author Hu 18 * 19 */ 20 public class CourseDao { 21 22 /** 23 * 添加 24 * @param course 25 * @return 26 */ 27 public boolean add(Course course) { 28 String sql = "insert into course(name, teacher, classroom) values('" + course.getName() + "','" + course.getTeacher() + "','" + course.getClassroom() + "')"; 29 //创建数据库链接 30 Connection conn = DBUtil.getConn(); 31 Statement state = null; 32 boolean f = false; 33 int a = 0; 34 35 try { 36 state = conn.createStatement(); 37 state.executeUpdate(sql); 38 } catch (Exception e) { 39 e.printStackTrace(); 40 } finally { 41 //关闭连接 42 DBUtil.close(state, conn); 43 } 44 45 if (a > 0) { 46 f = true; 47 } 48 return f; 49 } 50 51 /** 52 * 删除 53 * 54 * @param id 55 * @return 56 */ 57 public boolean delete (int id) { 58 boolean f = false; 59 String sql = "delete from course where id='" + id + "'"; 60 61 Connection conn = DBUtil.getConn(); 62 Statement state = null; 63 int a = 0; 64 65 try { 66 state = conn.createStatement(); 67 a = state.executeUpdate(sql); 68 } catch (SQLException e) { 69 e.printStackTrace(); 70 } finally { 71 DBUtil.close(state, conn); 72 } 73 74 if (a > 0) { 75 f = true; 76 } 77 return f; 78 } 79 80 /** 81 * 修改 82 * @param name 83 * @param pass 84 */ 85 public boolean update(Course course) { 86 String sql = "update course set name='" + course.getName() + "', teacher='" + course.getTeacher() + "', classroom='" + course.getClassroom() 87 + "' where id='" + course.getId() + "'"; 88 Connection conn = DBUtil.getConn(); 89 Statement state = null; 90 boolean f = false; 91 int a = 0; 92 93 try { 94 state = conn.createStatement(); 95 a = state.executeUpdate(sql); 96 } catch (SQLException e) { 97 e.printStackTrace(); 98 } finally { 99 DBUtil.close(state, conn); 100 } 101 102 if (a > 0) { 103 f = true; 104 } 105 return f; 106 } 107 108 /** 109 * 验证课程名称是否唯一 110 * true --- 不唯一 111 * @param name 112 * @return 113 */ 114 public boolean name(String name) { 115 boolean flag = false; 116 String sql = "select name from course where name = '" + name + "'"; 117 Connection conn = DBUtil.getConn(); 118 Statement state = null; 119 ResultSet rs = null; 120 121 try { 122 state = conn.createStatement(); 123 rs = state.executeQuery(sql); 124 while (rs.next()) { 125 flag = true; 126 } 127 } catch (SQLException e) { 128 e.printStackTrace(); 129 } finally { 130 DBUtil.close(rs, state, conn); 131 } 132 return flag; 133 } 134 135 /** 136 * 通过ID得到课程信息 137 * @param id 138 * @return 139 */ 140 public Course getCourseById(int id) { 141 String sql = "select * from course where id ='" + id + "'"; 142 Connection conn = DBUtil.getConn(); 143 Statement state = null; 144 ResultSet rs = null; 145 Course course = null; 146 147 try { 148 state = conn.createStatement(); 149 rs = state.executeQuery(sql); 150 while (rs.next()) { 151 String name = rs.getString("name"); 152 String teacher = rs.getString("teacher"); 153 String classroom = rs.getString("classroom"); 154 course = new Course(id, name, teacher, classroom); 155 } 156 } catch (Exception e) { 157 e.printStackTrace(); 158 } finally { 159 DBUtil.close(rs, state, conn); 160 } 161 162 return course; 163 } 164 165 /** 166 * 通过name得到Course 167 * @param name 168 * @return 169 */ 170 public Course getCourseByName(String name) { 171 String sql = "select * from course where name ='" + name + "'"; 172 Connection conn = DBUtil.getConn(); 173 Statement state = null; 174 ResultSet rs = null; 175 Course course = null; 176 177 try { 178 state = conn.createStatement(); 179 rs = state.executeQuery(sql); 180 while (rs.next()) { 181 int id = rs.getInt("id"); 182 String teacher = rs.getString("teacher"); 183 String classroom = rs.getString("classroom"); 184 course = new Course(id, name, teacher, classroom); 185 } 186 } catch (Exception e) { 187 e.printStackTrace(); 188 } finally { 189 DBUtil.close(rs, state, conn); 190 } 191 192 return course; 193 } 194 195 /** 196 * 查找 197 * @param name 198 * @param teacher 199 * @param classroom 200 * @return 201 */ 202 public List<Course> search(String name, String teacher, String classroom) { 203 String sql = "select * from course where "; 204 if (name != "") { 205 sql += "name like '%" + name + "%'"; 206 } 207 if (teacher != "") { 208 sql += "teacher like '%" + teacher + "%'"; 209 } 210 if (classroom != "") { 211 sql += "classroom like '%" + classroom + "%'"; 212 } 213 List<Course> list = new ArrayList<>(); 214 Connection conn = DBUtil.getConn(); 215 Statement state = null; 216 ResultSet rs = null; 217 218 try { 219 state = conn.createStatement(); 220 rs = state.executeQuery(sql); 221 Course bean = null; 222 while (rs.next()) { 223 int id = rs.getInt("id"); 224 String name2 = rs.getString("name"); 225 String teacher2 = rs.getString("teacher"); 226 String classroom2 = rs.getString("classroom"); 227 bean = new Course(id, name2, teacher2, classroom2); 228 list.add(bean); 229 } 230 } catch (SQLException e) { 231 e.printStackTrace(); 232 } finally { 233 DBUtil.close(rs, state, conn); 234 } 235 236 return list; 237 } 238 239 /** 240 * 全部数据 241 * @param name 242 * @param teacher 243 * @param classroom 244 * @return 245 */ 246 public List<Course> list() { 247 String sql = "select * from course"; 248 List<Course> list = new ArrayList<>(); 249 Connection conn = DBUtil.getConn(); 250 Statement state = null; 251 ResultSet rs = null; 252 253 try { 254 state = conn.createStatement(); 255 rs = state.executeQuery(sql); 256 Course bean = null; 257 while (rs.next()) { 258 int id = rs.getInt("id"); 259 String name2 = rs.getString("name"); 260 String teacher2 = rs.getString("teacher"); 261 String classroom2 = rs.getString("classroom"); 262 bean = new Course(id, name2, teacher2, classroom2); 263 list.add(bean); 264 } 265 } catch (SQLException e) { 266 e.printStackTrace(); 267 } finally { 268 DBUtil.close(rs, state, conn); 269 } 270 271 return list; 272 } 273 274 }
1 //CourseService.java 2 package com.hjf.service; 3 4 import java.util.List; 5 6 import com.hjf.dao.CourseDao; 7 import com.hjf.entity.Course; 8 9 /** 10 * CourseService 11 * 服务层 12 * @author Hu 13 * 14 */ 15 public class CourseService { 16 17 CourseDao cDao = new CourseDao(); 18 19 /** 20 * 添加 21 * @param course 22 * @return 23 */ 24 public boolean add(Course course) { 25 boolean f = false; 26 if(!cDao.name(course.getName())) { 27 cDao.add(course); 28 f = true; 29 } 30 return f; 31 } 32 33 /** 34 * 删除 35 */ 36 public void del(int id) { 37 cDao.delete(id); 38 } 39 40 /** 41 * 修改 42 * @return 43 */ 44 public void update(Course course) { 45 cDao.update(course); 46 } 47 48 /** 49 * 通过ID得到一个Course 50 * @return 51 */ 52 public Course getCourseById(int id) { 53 return cDao.getCourseById(id); 54 } 55 56 /** 57 * 通过Name得到一个Course 58 * @return 59 */ 60 public Course getCourseByName(String name) { 61 return cDao.getCourseByName(name); 62 } 63 64 /** 65 * 查找 66 * @return 67 */ 68 public List<Course> search(String name, String teacher, String classroom) { 69 return cDao.search(name, teacher, classroom); 70 } 71 72 /** 73 * 全部数据 74 * @return 75 */ 76 public List<Course> list() { 77 return cDao.list(); 78 } 79 }
1 //CourseServlet.java 2 package com.hjf.servlet; 3 4 import java.io.IOException; 5 import java.util.List; 6 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 import com.hjf.entity.Course; 14 import com.hjf.service.CourseService; 15 16 @WebServlet("/CourseServlet") 17 public class CourseServlet extends HttpServlet { 18 19 private static final long serialVersionUID = 1L; 20 21 CourseService service = new CourseService(); 22 23 /** 24 * 方法选择 25 */ 26 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 27 req.setCharacterEncoding("utf-8"); 28 String method = req.getParameter("method"); 29 30 if ("add".equals(method)) { 31 add(req, resp); 32 } else if ("del".equals(method)) { 33 del(req, resp); 34 } else if ("update".equals(method)) { 35 update(req, resp); 36 } else if ("search".equals(method)) { 37 search(req, resp); 38 } else if ("getcoursebyid".equals(method)) { 39 getCourseById(req, resp); 40 } else if ("getcoursebyname".equals(method)) { 41 getCourseByName(req, resp); 42 } else if ("list".equals(method)) { 43 list(req, resp); 44 } 45 } 46 47 /** 48 * 添加 49 * @param req 50 * @param resp 51 * @throws IOException 52 * @throws ServletException 53 */ 54 private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { 55 req.setCharacterEncoding("utf-8"); 56 //获取数据 57 String name = req.getParameter("name"); 58 String teacher = req.getParameter("teacher"); 59 String classroom = req.getParameter("classroom"); 60 Course course = new Course(name, teacher, classroom); 61 62 //添加后消息显示 63 if(service.add(course)) { 64 req.setAttribute("message", "添加成功"); 65 req.getRequestDispatcher("add.jsp").forward(req,resp); 66 } else { 67 req.setAttribute("message", "课程名称重复,请重新录入"); 68 req.getRequestDispatcher("add.jsp").forward(req,resp); 69 } 70 } 71 72 /** 73 * 全部 74 * @param req 75 * @param resp 76 * @throws ServletException 77 */ 78 private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 79 req.setCharacterEncoding("utf-8"); 80 81 List<Course> courses = service.list(); 82 req.setAttribute("courses", courses); 83 req.getRequestDispatcher("list.jsp").forward(req,resp); 84 } 85 86 /** 87 * 通过ID得到Course 88 * @param req 89 * @param resp 90 * @throws ServletException 91 */ 92 private void getCourseById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 93 req.setCharacterEncoding("utf-8"); 94 int id = Integer.parseInt(req.getParameter("id")); 95 Course course = service.getCourseById(id); 96 req.setAttribute("course", course); 97 req.getRequestDispatcher("detail2.jsp").forward(req,resp); 98 } 99 100 /** 101 * 通过名字查找 102 * 跳转至删除 103 * @param req 104 * @param resp 105 * @throws IOException 106 * @throws ServletException 107 */ 108 private void getCourseByName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 109 req.setCharacterEncoding("utf-8"); 110 String name = req.getParameter("name"); 111 Course course = service.getCourseByName(name); 112 if(course == null) { 113 req.setAttribute("message", "查无此课程!"); 114 req.getRequestDispatcher("del.jsp").forward(req,resp); 115 } else { 116 req.setAttribute("course", course); 117 req.getRequestDispatcher("detail.jsp").forward(req,resp); 118 } 119 } 120 121 /** 122 * 删除 123 * @param req 124 * @param resp 125 * @throws IOException 126 * @throws ServletException 127 */ 128 private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 129 req.setCharacterEncoding("utf-8"); 130 int id = Integer.parseInt(req.getParameter("id")); 131 service.del(id); 132 req.setAttribute("message", "删除成功!"); 133 req.getRequestDispatcher("del.jsp").forward(req,resp); 134 } 135 136 /** 137 * 修改 138 * @param req 139 * @param resp 140 * @throws IOException 141 * @throws ServletException 142 */ 143 private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 144 req.setCharacterEncoding("utf-8"); 145 int id = Integer.parseInt(req.getParameter("id")); 146 String name = req.getParameter("name"); 147 String teacher = req.getParameter("teacher"); 148 String classroom = req.getParameter("classroom"); 149 Course course = new Course(id, name, teacher, classroom); 150 151 service.update(course); 152 req.setAttribute("message", "修改成功"); 153 req.getRequestDispatcher("CourseServlet?method=list").forward(req,resp); 154 } 155 156 /** 157 * 查找 158 * @param req 159 * @param resp 160 * @throws ServletException 161 */ 162 private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ 163 req.setCharacterEncoding("utf-8"); 164 String name = req.getParameter("name"); 165 String teacher = req.getParameter("teacher"); 166 String classroom = req.getParameter("classroom"); 167 List<Course> courses = service.search(name, teacher, classroom); 168 req.setAttribute("courses", courses); 169 req.getRequestDispatcher("searchlist.jsp").forward(req,resp); 170 } 171 }
1 //DBUtil.java 2 package com.hjf.util; 3 4 import java.sql.Connection; 5 import java.sql.DriverManager; 6 import java.sql.PreparedStatement; 7 import java.sql.ResultSet; 8 import java.sql.SQLException; 9 import java.sql.Statement; 10 11 /** 12 * 数据库连接工具 13 * @author Hu 14 * 15 */ 16 public class DBUtil { 17 18 public static String db_url = "jdbc:mysql://localhost:3306/course"; 19 public static String db_user = "root"; 20 public static String db_pass = "root"; 21 22 public static Connection getConn () { 23 Connection conn = null; 24 25 try { 26 Class.forName("com.mysql.jdbc.Driver");//加载驱动 27 conn = DriverManager.getConnection(db_url, db_user, db_pass); 28 } catch (Exception e) { 29 e.printStackTrace(); 30 } 31 32 return conn; 33 } 34 35 /** 36 * 关闭连接 37 * @param state 38 * @param conn 39 */ 40 public static void close (Statement state, Connection conn) { 41 if (state != null) { 42 try { 43 state.close(); 44 } catch (SQLException e) { 45 e.printStackTrace(); 46 } 47 } 48 49 if (conn != null) { 50 try { 51 conn.close(); 52 } catch (SQLException e) { 53 e.printStackTrace(); 54 } 55 } 56 } 57 58 public static void close (ResultSet rs, Statement state, Connection conn) { 59 if (rs != null) { 60 try { 61 rs.close(); 62 } catch (SQLException e) { 63 e.printStackTrace(); 64 } 65 } 66 67 if (state != null) { 68 try { 69 state.close(); 70 } catch (SQLException e) { 71 e.printStackTrace(); 72 } 73 } 74 75 if (conn != null) { 76 try { 77 conn.close(); 78 } catch (SQLException e) { 79 e.printStackTrace(); 80 } 81 } 82 } 83 84 public static void main(String[] args) throws SQLException { 85 Connection conn = getConn(); 86 PreparedStatement pstmt = null; 87 ResultSet rs = null; 88 String sql ="select * from course"; 89 pstmt = conn.prepareStatement(sql); 90 rs = pstmt.executeQuery(); 91 if(rs.next()){ 92 System.out.println("空"); 93 }else{ 94 System.out.println("不空"); 95 } 96 } 97 }
1 //add.jsp 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>Insert title here</title> 9 10 </head> 11 <body> 12 <% 13 Object message = request.getAttribute("message"); 14 if(message!=null && !"".equals(message)){ 15 16 %> 17 <script type="text/javascript"> 18 alert("<%=request.getAttribute("message")%>"); 19 </script> 20 <%} %> 21 <div align="center"> 22 <h1 style="color: black;">课程信息录入</h1> 23 <a href="index.jsp">返回主页</a> 24 <form action="CourseServlet?method=add" method="post" onsubmit="return check()"> 25 <div class="a"> 26 课程名称<input type="text" id="name" name="name"/> 27 </div> 28 <div class="a"> 29 任课教师<input type="text" id="teacher" name="teacher" /> 30 </div> 31 <div class="a"> 32 上课地点<input type="text" id="classroom" name="classroom" /> 33 </div> 34 <div class="a"> 35 <button type="submit" class="b">保 存</button> 36 </div> 37 </form> 38 </div> 39 <script type="text/javascript"> 40 function check() { 41 var name = document.getElementById("name");; 42 var teacher = document.getElementById("teacher"); 43 var classroom = document.getElementById("classroom"); 44 45 //非空 46 if(name.value == '') { 47 alert('课程名称为空'); 48 name.focus(); 49 return false; 50 } 51 if(teacher.value == '') { 52 alert('教师为空'); 53 teacher.focus(); 54 return false; 55 } 56 if(classroom.value == '') { 57 alert('上课地点为空'); 58 classroom.focus(); 59 return false; 60 } 61 62 //教师 63 if(teacher.value != '王建民' && teacher.value != '王辉' && teacher.value != '刘丹' && teacher.value != '刘立嘉' && teacher.value != '杨子光'){ 64 alert('教师名称错误'); 65 return false; 66 } 67 68 //教室 69 if(!/^基教/.test(classroom.value) && !/^一教/.test(classroom.value) && !/^二教/.test(classroom.value) && !/^三教/.test(classroom.value)) { 70 alert('上课地点错误'); 71 return false; 72 } 73 } 74 </script> 75 </body> 76 </html>
1 //del.jsp 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>Insert title here</title> 9 10 </head> 11 <body> 12 <% 13 Object message = request.getAttribute("message"); 14 if(message!=null && !"".equals(message)){ 15 16 %> 17 <script type="text/javascript"> 18 alert("<%=request.getAttribute("message")%>"); 19 </script> 20 <%} %> 21 <div align="center"> 22 <h1 style="color: black;">课程信息删除</h1> 23 <a href="index.jsp">返回主页</a> 24 <form action="deleteservlet" method="post" onsubmit="return check()"> 25 <div class="a"> 26 课程名称<input type="text" id="classname" name="classname"/> 27 </div> 28 <div class="a"> 29 <button type="submit" class="b">查 找</button> 30 </div> 31 </form> 32 </div> 33 <script type="text/javascript"> 34 function check() { 35 var name = document.getElementById("classname");; 36 37 //非空 38 if(name.value == '') { 39 alert('课程名称为空'); 40 name.focus(); 41 return false; 42 } 43 } 44 </script> 45 </body> 46 </html>
1 //detail.jap 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>Insert title here</title> 9 <style> 10 .a{ 11 margin-top: 20px; 12 } 13 .b{ 14 font-size: 20px; 15 width: 160px; 16 color: white; 17 background-color: greenyellow; 18 } 19 .tb, td { 20 border: 1px solid black; 21 font-size: 22px; 22 } 23 </style> 24 </head> 25 <body> 26 <div align="center"> 27 <h1 style="color: black;">课程信息删除</h1> 28 <a href="index.jsp">返回主页</a> 29 <table class="tb"> 30 <tr> 31 <td>课程名称</td> 32 <td>${list.classname}</td> 33 </tr> 34 <tr> 35 <td>任课教师</td> 36 <td>${list.teachnamr}</td> 37 </tr> 38 <tr> 39 <td>上课地点</td> 40 <td>${list.site}</td> 41 </tr> 42 </table> 43 <div class="a"> 44 <a onclick="return check()" href="deleteservlet?method=post">删 除</a> 45 </div> 46 </div> 47 <script type="text/javascript"> 48 function check() { 49 if (confirm("真的要删除吗?")){ 50 return true; 51 }else{ 52 return false; 53 } 54 } 55 </script> 56 </body> 57 </html>
1 //detail2.jsp 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>Insert title here</title> 9 10 </head> 11 <body> 12 <% 13 Object message = request.getAttribute("message"); 14 if(message!=null && !"".equals(message)){ 15 16 %> 17 <script type="text/javascript"> 18 alert("<%=request.getAttribute("message")%>"); 19 </script> 20 <%} %> 21 <div align="center"> 22 <h1 style="color: black;">课程信息修改</h1> 23 <a href="index.jsp">返回主页</a> 24 <form action="CourseServlet?method=update" method="post" onsubmit="return check()"> 25 <div class="a"> 26 课程名称<input type="text" id="name" name="name" value="${course.name}"/> 27 </div> 28 <div class="a"> 29 任课教师<input type="text" id="teacher" name="teacher" value="${course.teacher}"/> 30 </div> 31 <div class="a"> 32 上课地点<input type="text" id="classroom" name="classroom" value="${course.classroom}"/> 33 </div> 34 <input type="hidden" id="id" name="id" value="${course.id}"/> 35 <div class="a"> 36 <button type="submit" class="b">修 改</button> 37 </div> 38 </form> 39 </div> 40 <script type="text/javascript"> 41 function check() { 42 var name = document.getElementById("name");; 43 var teacher = document.getElementById("teacher"); 44 var classroom = document.getElementById("classroom"); 45 46 //非空 47 if(name.value == '') { 48 alert('课程名称为空'); 49 name.focus(); 50 return false; 51 } 52 if(teacher.value == '') { 53 alert('教师为空'); 54 teacher.focus(); 55 return false; 56 } 57 if(classroom.value == '') { 58 alert('上课地点为空'); 59 classroom.focus(); 60 return false; 61 } 62 63 //教师 64 if(teacher.value != '王建民' && teacher.value != '王辉' && teacher.value != '刘丹' && teacher.value != '刘立嘉' && teacher.value != '杨子光'){ 65 alert('教师名称错误'); 66 return false; 67 } 68 69 //教室 70 if(!/^基教/.test(classroom.value) && !/^一教/.test(classroom.value) && !/^二教/.test(classroom.value) && !/^三教/.test(classroom.value)) { 71 alert('上课地点错误'); 72 return false; 73 } 74 } 75 </script> 76 </body> 77 </html>
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html> 4 <html> 5 <head> 6 <meta charset="UTF-8"> 7 <title>首页</title> 8 9 </head> 10 <body> 11 <div align="center"> 12 13 <div class="a"> 14 <a href="add.jsp">课程信息添加</a> 15 </div> 16 <div class="a"> 17 <a href="CourseServlet?method=list">课程信息修改</a> 18 </div> 19 <div class="a"> 20 <a href="del.jsp">课程信息删除</a> 21 </div> 22 <div class="a"> 23 <a href="search.jsp">课程信息查询</a> 24 </div> 25 </div> 26 </body> 27 </html>
1 //list.jsp 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 5 <!DOCTYPE html> 6 <html> 7 <head> 8 <meta charset="UTF-8"> 9 <title>Insert title here</title> 10 11 </head> 12 <body> 13 <% 14 Object message = request.getAttribute("message"); 15 if(message!=null && !"".equals(message)){ 16 17 %> 18 <script type="text/javascript"> 19 alert("<%=request.getAttribute("message")%>"); 20 </script> 21 <%} %> 22 <div align="center"> 23 <h1 style="color: black;">课程信息列表</h1> 24 <a href="index.jsp">返回主页</a> 25 <table class="tb"> 26 <tr> 27 <td>id</td> 28 <td>课程名称</td> 29 <td>任课教师</td> 30 <td>上课地点</td> 31 <td align="center" colspan="2">操作</td> 32 </tr> 33 <c:forEach items="${courses}" var="item"> 34 <tr> 35 <td>${item.id}</td> 36 <td>${item.name}</td> 37 <td>${item.teacher}</td> 38 <td>${item.classroom}</td> 39 <td><a href="CourseServlet?method=getcoursebyid&id=${item.id}">修改</a></td> 40 </tr> 41 </c:forEach> 42 </table> 43 </div> 44 </body> 45 </html>
1 //search.jsp 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 <meta charset="UTF-8"> 8 <title>Insert title here</title> 9 10 </head> 11 <body> 12 <div align="center"> 13 <h1 style="color: black;">课程信息查询</h1> 14 <a href="index.jsp">返回主页</a> 15 <form action="CourseServlet?method=search" method="post" onsubmit="return check()"> 16 <div class="a"> 17 课程名称<input type="text" id="name" name="name"/> 18 </div> 19 <div class="a"> 20 任课教师<input type="text" id="teacher" name="teacher" /> 21 </div> 22 <div class="a"> 23 上课地点<input type="text" id="classroom" name="classroom" /> 24 </div> 25 <div class="a"> 26 <button type="submit" class="b">查 询</button> 27 </div> 28 </form> 29 </div> 30 <script type="text/javascript"> 31 function check() { 32 var name = document.getElementById("name");; 33 var teacher = document.getElementById("teacher"); 34 var classroom = document.getElementById("classroom"); 35 36 //非空 37 if(name.value == '' && teacher.value == '' && classroom.value == '') { 38 alert('请填写一个条件'); 39 return false; 40 } 41 } 42 </script> 43 </body> 44 </html>
1 //searchlist.jsp 2 <%@ page language="java" contentType="text/html; charset=UTF-8" 3 pageEncoding="UTF-8"%> 4 <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 5 <!DOCTYPE html> 6 <html> 7 <head> 8 <meta charset="UTF-8"> 9 <title>Insert title here</title> 10 11 </head> 12 <body> 13 <div align="center"> 14 <h1 style="color: black;">课程信息列表</h1> 15 <a href="index.jsp">返回主页</a> 16 <table class="tb"> 17 <tr> 18 <td>id</td> 19 <td>课程名称</td> 20 <td>任课教师</td> 21 <td>上课地点</td> 22 </tr> 23 <!-- forEach遍历出adminBeans --> 24 <c:forEach items="${courses}" var="item" varStatus="status"> 25 <tr> 26 <td>${item.id}</td> 27 <td><a>${item.name}</a></td> 28 <td>${item.teacher}</td> 29 <td>${item.classroom}</td> 30 </tr> 31 </c:forEach> 32 </table> 33 </div> 34 </body> 35 </html>
应该就可以运行了,中间没有写的就是tomcat的安装了,大家可以在网上找一下,比较简单。