新建一个javaweb工程,如图新建包与类
源代码如下
CourseDao.java:
package src.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 src.entity.Course; import src.util.DBUtil; public class CourseDao { public boolean add(Course course) { String sql = "insert into lianxi(name, sex, classroom,time,age,mianmao,fuwu) values(‘" + course.getName() + "‘,‘" + course.getSex() + "‘,‘" + course.getClassroom() +"‘,‘" + course.getTime() + "‘,‘" + course.getAge() + "‘,‘" + course.getMianmao() + "‘,‘" + course.getFuwu() + "‘)"; Connection conn = DBUtil.getConn(); Statement state = null; boolean f = false; int a = 0; try { state = conn.createStatement(); a=state.executeUpdate(sql); } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } if (a > 0) { f = true; } return f; } public boolean delete (int id) { boolean f = false; String sql = "delete from lianxi 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; } public boolean update(Course course) { String sql = "update lianxi set name=‘" + course.getName() + "‘, sex=‘" + course.getSex() + "‘, classroom=‘" + course.getClassroom()+ "‘, time=‘" + course.getTime()+ "‘, age=‘" + course.getAge()+ "‘, mianmao=‘" + course.getMianmao()+ "‘, fuwu=‘" + course.getFuwu() + "‘ 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; } public boolean name(String name) { boolean flag = false; String sql = "select name from lianxi where name = ‘" + 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; } public Course getCourseById(int id) { String sql = "select * from lianxi where id =‘" + id + "‘"; 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 name = rs.getString("name"); String sex = rs.getString("sex"); String classroom = rs.getString("classroom"); String time = rs.getString("time"); String age = rs.getString("age"); String mianmao = rs.getString("mianmao"); String fuwu = rs.getString("fuwu"); course = new Course(id, name, sex, classroom, time, age, mianmao, fuwu); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return course; } public Course getCourseByName(String name) { String sql = "select * from lianxi 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()) { int id = rs.getInt("id"); String sex = rs.getString("sex"); String classroom = rs.getString("classroom"); String time = rs.getString("time"); String age = rs.getString("age"); String mianmao = rs.getString("mianmao"); String fuwu = rs.getString("fuwu"); course = new Course(id, name, sex, classroom, time, age, mianmao, fuwu); } } catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return course; } public List<Course> search(String name, String sex, String classroom,String time,String age,String mianmao,String fuwu) { String sql = "select * from lianxi where "; if (name != "") { sql += "name like ‘%" + name + "%‘"; } if (sex != "") { sql += "sex like ‘%" + sex + "%‘"; } if (classroom != "") { sql += "classroom like ‘%" + classroom + "%‘"; } if (time!= "") { sql += "time like ‘%" + time + "%‘"; } if (age!= "") { sql += "age like ‘%" + age + "%‘"; } if (mianmao!= "") { sql += "mianmao like ‘%" + mianmao + "%‘"; } if (fuwu!= "") { sql += "fuwu like ‘%" + fuwu + "%‘"; } 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()) { int id = rs.getInt("id"); String name2 = rs.getString("name"); String sex2 = rs.getString("sex"); String classroom2 = rs.getString("classroom"); String time2 = rs.getString("time"); String age2 = rs.getString("age"); String mianmao2 = rs.getString("mianmao"); String fuwu2 = rs.getString("fuwu"); bean = new Course(id, name2, sex2,classroom2, time2, age2, mianmao2, fuwu2); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } public List<Course> list() { String sql = "select * from lianxi"; 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()) { int id = rs.getInt("id"); String name2 = rs.getString("name"); String sex2 = rs.getString("sex"); String classroom2 = rs.getString("classroom"); String time2 = rs.getString("time"); String age2 = rs.getString("age"); String mianmao2 = rs.getString("mianmao"); String fuwu2 = rs.getString("fuwu"); bean = new Course(id, name2, sex2, classroom2, time2, age2, mianmao2, fuwu2); list.add(bean); } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return list; } }
Course.java:
package src.entity; public class Course { private int id; private String name; private String sex; private String classroom; private String time; private String age; private String mianmao; private String fuwu; public Course() {} public Course(int id, String name, String sex, String classroom,String time,String age,String mianmao,String fuwu) { this.setId(id); this.setName(name); this.setSex(sex); this.setClassroom(classroom); this.setTime(time); this.setAge(age); this.setMianmao(mianmao); this.setFuwu(fuwu); } public Course(String name, String sex, String classroom,String time,String age,String mianmao,String fuwu) { this.setName(name); this.setSex(sex); this.setClassroom(classroom); this.setTime(time); this.setAge(age); this.setMianmao(mianmao); this.setFuwu(fuwu); } public int getId() { return id; } public void setId(int id) { this.id = id; } 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 getClassroom() { return classroom; } public void setClassroom(String classroom) { this.classroom = classroom; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getMianmao() { return mianmao; } public void setMianmao(String mianmao) { this.mianmao = mianmao; } public String getFuwu() { return fuwu; } public void setFuwu(String fuwu) { this.fuwu = fuwu; } }
CourseServlet.java:
package src.servlet; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; 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 src.entity.Course; import src.dao.CourseDao; @WebServlet("/CourseServlet") public class CourseServlet extends HttpServlet { private static final long serialVersionUID = 1L; public CourseServlet() { super(); // TODO Auto-generated constructor stub } CourseDao dao = new CourseDao(); 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); } else if ("searchlist".equals(method)) { searchlist(req, resp); } } private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); String sex = req.getParameter("sex"); String classroom = req.getParameter("classroom"); Date currentTime=new Date(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String time = formatter.format(currentTime); String age = req.getParameter("age"); String mianmao = req.getParameter("mianmao"); String fuwu = req.getParameter("fuwu"); Course course = new Course(name, sex, classroom, time,age,mianmao,fuwu); Course course1 = dao.getCourseByName(name); if(course1==null) { dao.add(course); req.setAttribute("message", "注册成功"); req.getRequestDispatcher("add.jsp").forward(req,resp); } else { req.setAttribute("message", "用户名已注册"); req.getRequestDispatcher("add.jsp").forward(req,resp); } } private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); List<Course> courses = dao.list(); req.setAttribute("courses", courses); req.getRequestDispatcher("list.jsp").forward(req,resp); } private void searchlist(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); List<Course> courses = dao.list(); req.setAttribute("courses", courses); req.getRequestDispatcher("searchlist.jsp").forward(req,resp); } private void getCourseById(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); Course course = dao.getCourseById(id); req.setAttribute("course", course); req.getRequestDispatcher("detail2.jsp").forward(req,resp); } private void getCourseByName(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); Course course = dao.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); } } private void del(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); dao.delete(id); req.setAttribute("message", "删除成功"); req.getRequestDispatcher("del.jsp").forward(req,resp); } private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); int id = Integer.parseInt(req.getParameter("id")); String name = req.getParameter("name"); String sex = req.getParameter("sex"); String classroom = req.getParameter("classroom"); String time = req.getParameter("time"); String age = req.getParameter("age"); String mianmao = req.getParameter("mianmao"); String fuwu = req.getParameter("fuwu"); Course course = new Course(id,name, sex, classroom, time,age,mianmao,fuwu); dao.update(course); req.setAttribute("message", "修改成功"); req.getRequestDispatcher("CourseServlet?method=list").forward(req,resp); } private void search(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException{ req.setCharacterEncoding("utf-8"); String name = req.getParameter("name"); String sex = req.getParameter("sex"); String classroom = req.getParameter("classroom"); String time = req.getParameter("time"); String age = req.getParameter("age"); String mianmao = req.getParameter("mianmao"); String fuwu = req.getParameter("fuwu"); List<Course> courses = dao.search(name, sex, classroom, time,age,mianmao,fuwu); Course course1 = dao.getCourseByName(name); req.setAttribute("courses", courses); req.getRequestDispatcher("searchlist.jsp").forward(req,resp); } }
DBUtil.java:
package src.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtil { public static String db_url = "jdbc:mysql://localhost:3306/test?useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT"; public static String db_user = "root"; public static String db_pass = "root"; 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; } 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(); } } } }
如下图新建jsp文件
源代码如下
add.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>志愿者登记</title> <style> .a { border: 2px solid #a1a1a1; padding: 10px 40px; /* background:#dddddd; */ width: px; border-radius: 25px; } #bk { border: 3px solid #a1a1a1; padding: 10px 60px; /* background:#dddddd; */ width: px; border-radius: 25px; } .button { background-color: #ff8433; border: none; color: white; padding: 5px 10px; text-decoration: none; display: inline-block; font-size: 16px; margin: 2px 2px; cursor: pointer; } table{ margin: auto; } </style> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: red;">志愿者信息登记</h1> <form action="CourseServlet?method=add" method="post" onsubmit="return check()"> <table> <tr> <td>姓名</td> <td><input id="bk" type="text" name="name" ></td> </tr> <tr> <td>性别</td> <td><input type="radio" name="sex" value="男">男 <input type="radio" name="sex" value="女">女</td> </tr> <tr> <td>民族</td> <td><input id="bk" type="text" name="classroom"></td> </tr> <tr> <td>年龄</td> <td><input id="bk" type="text" name="age"></td> </tr> <tr> <td>政治面貌</td> <td><select style="width:200px;hight:20px;" name="mianmao"> <option value="群众">群众</option> <option value="共青团员">共青团员</option> <option value="共青党员">共青党员</option> </select></td> </tr> <tr> <td>服务类别</td> <td><input type="checkbox" name="fuwu" value="扶贫济困">扶贫济困<br> <input type="checkbox" name="fuwu" value="敬老助残">敬老助残 <br> <input type="checkbox" name="fuwu" value="社区服务">社区服务<br> <input type="checkbox" name="fuwu" value="秩序维护">秩序维护<br> <input type="checkbox" name="fuwu" value="文体服务">文体服务<br> <input type="checkbox" name="fuwu" value="环境保护">环境保护<br> <input type="checkbox" name="fuwu" value="治安防范">治安防范<br> <input type="checkbox" name="fuwu" value="医疗救治">医疗救治<br> <input type="checkbox" name="fuwu" value="法律援助">法律援助<br> <input type="checkbox" name="fuwu" value="大型活动">大型活动<br> <input type="checkbox" name="fuwu" value="心理疏导">心理疏导<br> <input type="checkbox" name="fuwu" value="精神抚慰">精神抚慰<br> <input type="checkbox" name="fuwu" value="支教支医">支教支医<br> <input type="checkbox" name="fuwu" value="科学普及">科学普及 <br> <input type="checkbox" name="fuwu" value="应急救援">应急救援<br> <input type="checkbox" name="fuwu" value="便民服务">便民服务<br> <input type="checkbox" name="fuwu" value="民事调解">民事调解<br> <input type="checkbox" name="fuwu" value="文明引导">文明引导<br> <input type="checkbox" name="fuwu" value="安全生产">安全生产<br> <input type="checkbox" name="fuwu" value="禁毒宣传">禁毒宣传<br> </tr> <tr> <td></td> <a href="index.jsp">返回主页面</a> <td><input class ="button" type="submit" value="注册"> <input class ="button" type="reset" value="重置"></td> <script type="text/javascript"> function check() { var name = document.getElementById("name"); var sex = document.getElementById("sex"); var classroom = document.getElementById("classroom"); var time = document.getElementById("time"); var age = document.getElementById("age"); var mianmao = document.getElementById("mianmao"); var fuwu = document.getElementById("fuwu"); //非空 if(name.value == ‘‘) { alert(‘志愿者姓名为空‘); name.focus(); return false; } if(sex.value == ‘‘) { alert(‘性别为空‘); sex.focus(); return false; } if(classroom.value == ‘‘) { alert(‘民族为空‘); classroom.focus(); return false; } if(time.value == ‘‘) { alert(‘注册时间为空‘); time.focus(); return false; } if(age.value == ‘‘) { alert(‘年龄为空‘); age.focus(); return false; } if(mianmao.value == ‘‘) { alert(‘政治面貌为空‘); mianmao.focus(); return false; } if(fuwu.value == ‘‘) { alert(‘服务类型为空‘); fuwu.focus(); return false; } } </script> </body> </html>
del.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>志愿者信息删除</title> <style> .a{ border: 2px solid #a1a1a1; padding: 10px 40px; width: px; border-radius: 25px; } #bk { border: 3px solid #a1a1a1; padding: 10px 60px; /* background:#dddddd; */ width: px; border-radius: 25px; } .button { background-color: #ff8433; border: none; color: white; padding: 5px 10px; text-decoration: none; display: inline-block; font-size: 16px; margin: 2px 2px; cursor: pointer; } </style> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: red;">志愿者信息删除</h1> <a href="index.jsp">返回主页</a> <form action="CourseServlet?method=getcoursebyName" method="post" onsubmit="return check()"> <tr> <td>姓名</td> <td><input id="bk" type="text" name="name" ></td><br> </tr> <td><input class ="button" type="submit" value="查询"></td> </form> </div> <script type="text/javascript"> function check() { var name = document.getElementById("name");; //非空 if(name.value == ‘‘) { alert(‘志愿者姓名为空‘); name.focus(); return false; } } </script> </body> </html>
detail.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>志愿者信息删除</title> <style> .a { border: 2px solid #a1a1a1; padding: 10px 40px; /* background:#dddddd; */ width: px; border-radius: 25px; } #bk { border: 3px solid #a1a1a1; padding: 10px 60px; /* background:#dddddd; */ width: px; border-radius: 25px; } .tb, td { border: 1px solid black; font-size: 22px; } .button { background-color: #ff8433; border: none; color: white; padding: 5px 10px; text-decoration: none; display: inline-block; font-size: 16px; margin: 2px 2px; cursor: pointer; } </style> </head> <body> <div align="center"> <h1 style="color: red;">志愿者信息删除</h1> <a href="index.jsp">返回主页</a> <table class="tb"> <tr> <td>志愿者姓名</td> <td>${course.name}</td> </tr> <tr> <td>性别</td> <td>${course.sex}</td> </tr> <tr> <td>民族</td> <td>${course.classroom}</td> </tr> <tr> <td>注册时间</td> <td>${course.time}</td> </tr> <tr> <td>年龄</td> <td>${course.age}</td> </tr> <tr> <td>政治面貌</td> <td>${course.mianmao}</td> </tr> <tr> <td>服务类型</td> <td>${course.fuwu}</td> </tr> </table> <div > <a onclick="return check()" href="CourseServlet?method=del&id=${course.id}"><input class ="button" type="submit" value="删除"></a> </div> </div> <script type="text/javascript"> function check() { if (confirm("真的要删除吗?")){ return true; }else{ return false; } } </script> </body> </html>
detail2.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>志愿者信息修改</title> <style> .a { border: 2px solid #a1a1a1; padding: 10px 40px; /* background:#dddddd; */ width: px; border-radius: 25px; } #bk { border: 3px solid #a1a1a1; padding: 10px 60px; /* background:#dddddd; */ width: px; border-radius: 25px; } .button { background-color: #ff8433; border: none; color: white; padding: 5px 10px; text-decoration: none; display: inline-block; font-size: 16px; margin: 2px 2px; cursor: pointer; } table{ margin: auto; } </style> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: red;">志愿者信息修改</h1> <a href="index.jsp">返回主页</a> <form action="CourseServlet?method=update" method="post" onsubmit="return check()"> <table> <tr> <td>姓名</td> <td><input id="bk" type="text" name="name" value="${course.name}"></td> </tr> <tr> <td>性别</td> <td><input type="radio" name="sex" value="男">男 <input type="radio" name="sex" value="女">女</td> </tr> <tr> <td>民族</td> <td><input id="bk" type="text" name="classroom" value="${course.classroom}"></td> </tr> <tr> <td>注册时间</td> <td><input id="bk" type="text" name="time" value="${course.time}"/></td> <tr> <td>年龄</td> <td><input id="bk" type="text" name="age" value="${course.age}"></td> </tr> <tr> <td>政治面貌</td> <td><select style="width:200px;hight:20px;" name="mianmao"> <option value="${course.mianmao}">${course.mianmao}</option> <option value="群众">群众</option> <option value="共青团员">共青团员</option> <option value="共青党员">共青党员</option> </select></td> </tr> <tr> <td>服务类别</td> <td><input type="checkbox" name="fuwu" value="扶贫济困">扶贫济困<br> <input type="checkbox" name="fuwu" value="敬老助残">敬老助残 <br> <input type="checkbox" name="fuwu" value="社区服务">社区服务<br> <input type="checkbox" name="fuwu" value="秩序维护">秩序维护<br> <input type="checkbox" name="fuwu" value="文体服务">文体服务<br> <input type="checkbox" name="fuwu" value="环境保护">环境保护<br> <input type="checkbox" name="fuwu" value="治安防范">治安防范<br> <input type="checkbox" name="fuwu" value="医疗救治">医疗救治<br> <input type="checkbox" name="fuwu" value="法律援助">法律援助<br> <input type="checkbox" name="fuwu" value="大型活动">大型活动<br> <input type="checkbox" name="fuwu" value="心理疏导">心理疏导<br> <input type="checkbox" name="fuwu" value="精神抚慰">精神抚慰<br> <input type="checkbox" name="fuwu" value="支教支医">支教支医<br> <input type="checkbox" name="fuwu" value="科学普及">科学普及 <br> <input type="checkbox" name="fuwu" value="应急救援">应急救援<br> <input type="checkbox" name="fuwu" value="便民服务">便民服务<br> <input type="checkbox" name="fuwu" value="民事调解">民事调解<br> <input type="checkbox" name="fuwu" value="文明引导">文明引导<br> <input type="checkbox" name="fuwu" value="安全生产">安全生产<br> <input type="checkbox" name="fuwu" value="禁毒宣传">禁毒宣传<br> </tr> </table> <input type="hidden" id="id" name="id" value="${course.id}"/> <input class ="button" type="submit" value="修改"> </form> </div> <script type="text/javascript"> function check() { var name = document.getElementById("name"); var sex = document.getElementById("sex"); var classroom = document.getElementById("classroom"); var time = document.getElementById("time"); var age = document.getElementById("age"); var mianmao = document.getElementById("mianmao"); var fuwu = document.getElementById("fuwu"); //非空 if(name.value == ‘‘) { alert(‘志愿者姓名为空‘); name.focus(); return false; } if(sex.value == ‘‘) { alert(‘性别为空‘); sex.focus(); return false; } if(classroom.value == ‘‘) { alert(‘民族为空‘); classroom.focus(); return false; } if(time.value == ‘‘) { alert(‘注册时间为空‘); time.focus(); return false; } if(age.value == ‘‘) { alert(‘年龄为空‘); age.focus(); return false; } if(mianmao.value == ‘‘) { alert(‘政治面貌为空‘); mianmao.focus(); return false; } if(fuwu.value == ‘‘) { alert(‘服务类型为空‘); fuwu.focus(); return false; } //性别 if(sex.value != ‘男‘ && sex.value != ‘女‘ ){ alert(‘性别错误‘); return false; } //政治面貌 if(!/^群众/.test(mianmao.value) && !/^共青团员/.test(mianmao.value) && !/^中共党员/.test(mianmao.value)) { alert(‘政治面貌错误‘); return false; } } </script> </body> </html>
index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>首页</title> <style> .a{ font-size: 26px; margin-top: 20px; } </style> </head> <body> <div align="center"> <h1 style="color: red;">青年志愿者服务网</h1> <div class="a"> <a href="add.jsp">志愿者登记信息</a> </div> <div class="a"> <a href="CourseServlet?method=list">修改志愿者信息</a> </div> <div class="a"> <a href="del.jsp">删除志愿者信息</a> </div> <div class="a"> <a href="search.jsp">查询志愿者信息</a> </div> <div class="a"> <a href="CourseServlet?method=searchlist">志愿者信息浏览</a> </div> </div> </body> </html>
list.jsp:
<%@ 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> <html> <head> <meta charset="UTF-8"> <title>志愿者信息修改</title> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } .tb, td { border: 1px solid black; font-size: 22px; } </style> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: red;">志愿者信息列表</h1> <a href="index.jsp">返回主页</a> <table class="tb"> <tr> <td>id</td> <td>姓名</td> <td>性别</td> <td>民族</td> <td>注册时间</td> <td>年龄</td> <td>政治面貌</td> <td>服务类型</td> <td align="center" colspan="2">操作</td> </tr> <c:forEach items="${courses}" var="item"> <tr> <td>${item.id}</td> <td>${item.name}</td> <td>${item.sex}</td> <td>${item.classroom}</td> <td>${item.time}</td> <td>${item.age}</td> <td>${item.mianmao}</td> <td>${item.fuwu}</td> <td><a href="CourseServlet?method=getCourseById&id=${item.id}">修改</a></td> </tr> </c:forEach> </table> </div> </body> </html>
search.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <style> .a { border: 2px solid #a1a1a1; padding: 10px 40px; /* background:#dddddd; */ width: px; border-radius: 25px; } #bk { border: 3px solid #a1a1a1; padding: 10px 60px; /* background:#dddddd; */ width: px; border-radius: 25px; } .button { background-color: #ff8433; border: none; color: white; padding: 5px 10px; text-decoration: none; display: inline-block; font-size: 16px; margin: 2px 2px; cursor: pointer; } table{ margin: auto; } </style> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <div align="center"> <h1 style="color: red;">志愿者信息查询</h1> <a href="index.jsp">返回主页</a> <form action="CourseServlet?method=search" method="post" onsubmit="return check()"> <table> <tr> <td>姓名</td> <td><input id="bk" type="text" name="name" ></td> </tr> <tr> <td>性别</td> <td><input id="bk"type="text" name="sex"></td> </tr> <tr> <td>民族</td> <td><input id="bk" type="text" name="classroom"></td> </tr> <tr> <td>注册时间</td> <td><input id="bk" type="text" name="time"></td> </tr> <tr> <td>年龄</td> <td><input id="bk" type="text" name="age"></td> </tr> <tr> <td>政治面貌</td> <td><input id="bk" type="text" name="mianmao"></td> </tr> <tr> <td>服务类别</td> <td><input id="bk"type="text" name="fuwu" ><br> </tr> <tr> <td></td> <td><input class ="button" type="submit" value="查询"> <input class="button" type="reset" value="重置"></td> </form> </div> <script type="text/javascript"> function check() { var name = document.getElementById("name"); var sex = document.getElementById("sex"); var classroom = document.getElementById("classroom"); var time = document.getElementById("time"); var age = document.getElementById("age"); var mianmao = document.getElementById("mianmao"); var fuwu = document.getElementById("fuwu"); //非空 if(name.value == ‘‘ && sex.value == ‘‘ && classroom.value == ‘‘ && time.value == ‘‘ && age.value == ‘‘ && mianmao.value == ‘‘ && fuwu.value == ‘‘) { alert(‘请填写一个条件‘); return false; }else return true; } </script> </body> </html>
searchlist.jsp:
<%@ 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> <html> <head> <meta charset="UTF-8"> <title>志愿者信息浏览</title> <style> .a{ margin-top: 20px; } .b{ font-size: 20px; width: 160px; color: white; background-color: greenyellow; } .tb, td { border: 1px solid black; font-size: 22px; } </style> </head> <body> <div align="center"> <h1 style="color: red;">志愿者信息列表</h1> <a href="index.jsp">返回主页</a> <table class="tb"> <tr> <td>id</td> <td>姓名</td> <td>性别</td> <td>民族</td> <td>注册时间</td> <td>年龄</td> <td>政治面貌</td> <td>服务类型</td> </tr> <!-- forEach遍历出adminBeans --> <c:forEach items="${courses}" var="item" varStatus="status"> <tr> <td>${item.id}</td> <td><a>${item.name}</a></td> <td>${item.sex}</td> <td>${item.classroom}</td> <td>${item.time}</td> <td>${item.age}</td> <td>${item.mianmao}</td> <td>${item.fuwu}</td> </tr> </c:forEach> </table> </div> </body> </html>
MySQL数据库中表名为”lianxi“
表内详细信息如下:
原文:https://www.cnblogs.com/xhj1074376195/p/12088196.html