大体上和2019级的基本相同,可以说是换汤不换药
个人写的比较拙劣,基本完成了题目要求,发出来供大家参考,如有需要也可参考19级的第七次人口普查的代码
话不多说上代码
bean:
package bean; public class Bean { private String theme; private String mudi; private String leixing; private String time; private String address; private String object; private String neirong; private String anpai; public Bean(String theme, String mudi, String leixing, String time, String address, String object, String neirong, String anpai) { // TODO 自动生成的构造函数存根 this.theme=theme; this.mudi=mudi; this.leixing=leixing; this.time=time; this.address=address; this.object=object; this.neirong=neirong; this.anpai=anpai; } public String getTheme() { return theme; } public void setTheme(String theme) { this.theme = theme; } public String getMudi() { return mudi; } public void setMudi(String mudi) { this.mudi = mudi; } public String getLeixing() { return leixing; } public void setLeixing(String leixing) { this.leixing = leixing; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } public String getObject() { return object; } public void setObject(String object) { this.object = object; } public String getNeirong() { return neirong; } public void setNeirong(String neirong) { this.neirong = neirong; } public String getAnpai() { return anpai; } public void setAnpai(String anpai) { this.anpai = anpai; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
dao:
package dao; import java.sql.*; import bean.Bean; import util.Util; public class Dao { /** * 添加 * * @param id * @return */ public int add(Bean bean) { String sql="insert into `shetuan`(theme,mudi,leixing,time,address,object,neirong,anpai) values('" + bean.getTheme() + "','" + bean.getMudi() + "','" + bean.getLeixing() + "','" + bean.getTime()+"','"+ bean.getAddress()+"','"+bean.getObject()+"','"+bean.getNeirong()+"','"+bean.getAnpai()+"')"; //创建数据库链接 Connection conn = Util.getConn(); Statement state = null; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); /* * 对下方注释处的解释:检查SQL语句是否正确执行,可删除,因此我在其余功能在检验之后将其删除了 */ //System.out.println(a); } catch (Exception e) { e.printStackTrace(); } finally { //关闭连接 Util.close(state, conn); } return a; } /** * 删除 * * @param id * @return */ public int delete (String theme) { String sql = "delete from `shetuan` where theme='" + theme + "'"; Connection conn = Util.getConn(); Statement state = null; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { Util.close(state, conn); } return a; } /** * 修改 * * @param id * @return */ public int update (Bean bean) { String sql = "update shetuan set mudi='" + bean.getMudi() + "', leixing='" + bean.getLeixing() +"', time='" + bean.getTime() +"', address='" + bean.getAddress() +"', object='" + bean.getObject() +"', neirong='" + bean.getNeirong() + "', anpai='" + bean.getAnpai() + "' where theme='" + bean.getTheme() + "'"; Connection conn = Util.getConn(); Statement state = null; int a = 0; try { state = conn.createStatement(); a = state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { Util.close(state, conn); } return a; } /** * 查询 * * @param id * @return */ public static Bean Search(String theme1,String time1,String leixing1,String object1) { String sql = "select * from shetuan where "; /* * 此处为模糊查找 */ if (theme1 != "") { sql += "theme like '%" + theme1 + "%'"; } else if (time1 != "") { sql += "time like '%" + time1 + "%'"; } else if (leixing1 != "") { sql += "leixing like '%" + leixing1 + "%'"; } else if (object1 != "") { sql += "object like '%" + object1 + "%'"; } else { } Connection conn = Util.getConn(); Statement state = null; ResultSet rs = null; Bean bean=null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String theme = rs.getString("theme"); String mudi = rs.getString("mudi"); String leixing = rs.getString("leixing"); String time = rs.getString("time"); String address = rs.getString("address"); String object = rs.getString("object"); String neirong = rs.getString("neirong"); String anpai = rs.getString("anpai"); bean=new Bean(theme,mudi,leixing,time,address,object,neirong,anpai); } } catch (Exception e) { e.printStackTrace(); } finally { Util.close(rs, state, conn); } return bean; } }
util:
package util; import java.sql.*; public class Util { public static String db_url = "jdbc:mysql://localhost:3306/2022?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone = GMT&allowPublicKeyRetrieval=true"; public static String db_user = "root"; public static String db_pass = "zbw170910"; public static Connection getConn () { Connection conn = null; try { Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动 System.out.println("连接成功");//检查数据库是否连接,可删除 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(); } } } public static void main(String[] args) throws SQLException { } }
service:
package service; import bean.Bean; import dao.Dao; public class Service { Dao dao=new Dao(); public int add(Bean bean) { // TODO 自动生成的方法存根 String a=bean.getTheme(); String b=bean.getMudi(); String c=bean.getNeirong(); String d=bean.getAnpai(); /* * 用于输入字数判定,过长过短或者判空都可以在这里写 * 理论上在jsp界面写也可以但是我不会,大家有需要自行查询然后私信我 */ if(a.length()>20) return 2; if(b.length()>50) return 3; if(c.length()>500) return 4; if(d.length()>500) return 5; if(a.length()==0) return 6; if(dao.add(bean)>0) { return 1; } else { return 0; } } public int delete(String theme) { // TODO 自动生成的方法存根 if(dao.delete(theme)>0) { return 1; } else { return 0; } } public int update(Bean bean) { // TODO 自动生成的方法存根 if(dao.update(bean)>0) { return 1; } else { return 0; } } public Bean search(String theme,String time,String leixing,String object) { return Dao.Search(theme,time,leixing,object); } }
servlet1:
package servlet1; import jakarta.servlet.ServletException; import jakarta.servlet.annotation.WebServlet; import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.io.IOException; import bean.Bean; import service.Service; /** * Servlet implementation class Servlet1 */ public class Servlet1 extends HttpServlet { private static final long serialVersionUID = 1L; Service service = new Service(); /** * 方法选择 */ 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("delete".equals(method)) { delete(req, resp); }else if("update".equals(method)) { update(req, resp); }else if("search".equals(method)) { search(req, resp); } } /** * 查询 * @param req * @param resp * @throws IOException * @throws ServletException */ private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO 自动生成的方法存根 req.setCharacterEncoding("utf-8"); String theme = req.getParameter("theme"); String time = req.getParameter("time"); String leixing = req.getParameter("leixing"); String object = req.getParameter("object"); Bean bean =service.search(theme,time,leixing,object); //查询信息显示,和其它三个功能不同的是,这个传进来的参数是Bean的实例化对象而不是一个数或者字符串 if(bean == null) { req.setAttribute("message", "查无此人!"); req.getRequestDispatcher("search.jsp").forward(req,resp); } else { req.setAttribute("bean", bean); req.getRequestDispatcher("search1.jsp").forward(req,resp); /* * 此if else语句作用相当于界面跳转,如果message为空就提示信息 “查无此人!”并留在search.jsp界面; * 如果bean有值就跳转到search1.jsp界面,输出查询信息 */ } } /** * 修改 * @param req * @param resp * @throws IOException * @throws ServletException */ private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { req.setCharacterEncoding("utf-8"); //获取数据 String theme = req.getParameter("theme"); String mudi = req.getParameter("mudi"); String leixing = req.getParameter("leixing"); String time = req.getParameter("time"); String address = req.getParameter("address"); /* * 对下方注释处的解释:要求活动对象为复选框,当多选时需要传入两个参数,所以要和其他变量区别开 */ //String object = req.getParameter("object"); String neirong = req.getParameter("neirong"); String anpai = req.getParameter("anpai"); String a1=null; String a2=null; if(req.getParameter("a1")!=null) { a1=req.getParameter("a1")+' '; } else a1=" "; if(req.getParameter("a2")!=null) { a2=req.getParameter("a2")+' '; } else a2=" "; String object=a1+a2; Bean bean = new Bean(theme,mudi,leixing,time,address,object,neirong,anpai); int a=service.update(bean); //修改后消息显示 if(a==1) { req.setAttribute("message", "修改成功"); req.getRequestDispatcher("update.jsp").forward(req,resp); } if(a==0){ req.setAttribute("message", "修改失败,请重新录入"); req.getRequestDispatcher("update.jsp").forward(req,resp); } } /** * 删除 * @param req * @param resp * @throws IOException * @throws ServletException */ private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ req.setCharacterEncoding("utf-8"); String theme = req.getParameter("theme"); int i =service.delete(theme); //删除后消息显示 if(i>0) { req.setAttribute("message", "删除成功"); req.getRequestDispatcher("delete.jsp").forward(req,resp); } else { req.setAttribute("message", "删除失败,未找到该活动内容"); req.getRequestDispatcher("delete.jsp").forward(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 theme = req.getParameter("theme"); String mudi = req.getParameter("mudi"); String leixing = req.getParameter("leixing"); String time = req.getParameter("time"); String address = req.getParameter("address"); /* * 此处为复选框,故需要传两个值 */ //String object = req.getParameter("object"); String neirong = req.getParameter("neirong"); String anpai = req.getParameter("anpai"); String a1=null; String a2=null; if(req.getParameter("a1")!=null) { a1=req.getParameter("a1")+' '; } else a1=" "; if(req.getParameter("a2")!=null) { a2=req.getParameter("a2")+' '; } else a2=" "; String object=a1+a2; Bean bean = new Bean(theme,mudi,leixing,time,address,object,neirong,anpai); int a=service.add(bean); //添加后消息显示 if(a==1) { req.setAttribute("message", "添加成功"); req.getRequestDispatcher("add.jsp").forward(req,resp); } if(a==0){ req.setAttribute("message", "添加失败,请重新录入"); req.getRequestDispatcher("add.jsp").forward(req,resp); } if(a==2) { req.setAttribute("message", "活动主题不能超过20字"); req.getRequestDispatcher("add.jsp").forward(req,resp); } if(a==3) { req.setAttribute("message", "活动目的不能超过50字"); req.getRequestDispatcher("add.jsp").forward(req,resp); } if(a==4) { req.setAttribute("message", "活动内容不能超过500字"); req.getRequestDispatcher("add.jsp").forward(req,resp); } if(a==5) { req.setAttribute("message", "活动日程安排不能超过500字"); req.getRequestDispatcher("add.jsp").forward(req,resp); } if(a==6) { req.setAttribute("message", "活动主题为空"); req.getRequestDispatcher("add.jsp").forward(req,resp); } } /** * @see HttpServlet#HttpServlet() */ public Servlet1() { super(); // TODO Auto-generated constructor stub } /** * @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) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
Menu.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>校园社团活动管理系统</title> </head> <body> <div align="center"> <p aligin="center"> <font size=5>欢迎使用校园社团活动管理系统</font> </p> <div class="a"> <a href="add.jsp"><font size=3>发布活动信息</font></a> </div> <div class="a"> <a href="update.jsp"><font size=3>修改活动信息</font></a> </div> <div class="a"> <a href="delete.jsp"><font size=3>删除活动信息</font></a> </div> <div class="a"> <a href="search.jsp"><font size=3>活动信息查询</font></a> </div> <div class="a"> <a href="liulan.jsp"><font size=3>活动信息浏览</font></a> </div> </div> </body> </html>
add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>发布活动信息</title> </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: black;">发布活动信息</h1> <a href="Menu.jsp">返回主页</a> <form name="Menu" action="Servlet1?method=add" method="post" onsubmit="return check()"> <div class="a"> 活动主题<input type="text" name="theme" /> </div> <div class="a"> 活动目的<input type="text" name="mudi" /> </div> <div class="a"> 活动类型 社团竞赛<input type="radio" id="leixing" name="leixing" value="社团竞赛" > 野外采风<input type="radio" id="leixing" name="leixing" value="野外采风" > 校内集会<input type="radio" id="leixing" name="leixing" value="校内集会" > 社团纳新<input type="radio" id="leixing" name="leixing" value="社团纳新" > </div> <div class="a"> 活动时间<input type="text" name="time" /> </div> <div class="a"> 活动地点<input type="text" name="address" /> </div> <div class="a"> 活动对象 <label> <input type="checkbox" name="a1" value="社团成员" />社团成员 <input type="checkbox" name="a2" value="全体师生" checked="checked"/>全体师生 </label> </div> <div class="a"> 活动内容<input type="text" name="neirong" /> </div> <div class="a"> 活动日程安排<input type="text" name="anpai" /> </div> <div class="a"> <input type="submit" value="提交"> <input type="reset" value="重置"> </div> </form> </div> </body> </html>
delete.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>删除活动信息</title> </head> <body> <div align="center"> <h1 style="color: black;">删除活动信息</h1> <form name="form" method="post" action="Menu.jsp"> <input type="submit" value="返回主页"> </form> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <form action="Servlet1?method=delete" method="post" onsubmit="return check()"> 输入要删除的活动主题: <input type="text" name="theme" /> <input type="submit" value="删除" /> </form> </div> </body> </html>
update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>修改活动信息</title> </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: black;">修改活动信息</h1> <a href="Menu.jsp">返回主页</a> <form name="Menu" action="Servlet1?method=update" method="post" onsubmit="return check()"> 活动主题: <label> <input type="text" name="theme" /> <br> </label> 活动目的: <label> <input type="text" name="mudi" /> <br> </label> 活动类型: <label> <input type="radio" name="leixing" value="社团竞赛" > 社团竞赛 <input type="radio" name="leixing" value="野外采风" >野外采风 <input type="radio" name="leixing" value="校内集会" > 校内集会 <input type="radio" name="leixing" value="社团纳新" >社团纳新 <br> </label> 活动时间: <label> <input type="text" name="time" /> <br> </label> 活动地点: <label> <input type="text" name="address" /> <br> </label> 活动对象: <label> <input type="checkbox" name="a1" value="社团人员" />社团人员 <input type="checkbox" name="a2" value="全体师生" />全体师生 <br> </label> 活动内容: <label> <input type="text" name="neirong" /> <br> </label> 活动安排: <label> <input type="text" name="anpai" /><br> </label> <input type="submit" value="修改" /> <input type="reset" value="重置" /> </form> </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>活动信息查询</title> </head> <body> <% Object message = request.getAttribute("message"); if(message!=null && !"".equals(message)){ %> <script type="text/javascript"> alert("<%=request.getAttribute("message")%>"); </script> <%} %> <form name="form" action="Servlet1?method=search" method="post" onsubmit="return check()"> <div align="center"> <h1 style="color: black;">活动信息查询</h1> </div> <table align="center" border="1" width="500"> <tr> <td>1.活动主题: </td> <td> <label> <input type="text" name="theme" /> </label> </td> </tr> <tr> <td>2.活动时间: </td> <td> <label> <input type="text" name="time" /> </label> </td> </tr> <tr> <td>3.活动类型: </td> <td> <label> <input type="text" name="leixing" /> </label> </td> </tr> <tr> <td>4.活动对象: </td> <td> <label> <input type="text" name="object" /> </label> </td> </tr> <tr align="center"> <td colspan="2"> <input type="submit" value="查询" /> <input type="reset" value="重置" /> </td> </tr> </table> </form> </body> </html>
search1.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>查询结果</title> </head> <body> <div align="center"> <h1 style="color: black;">查询结果显示</h1> </div> <table align="center" border="1" width="500"> <tr> <td>1.活动主题: </td> <td> <label> ${bean.theme} </label> </td> </tr> <tr> <td>2.活动目的: </td> <td> <label> ${bean.mudi} </label> </td> </tr> <tr> <td>3.活动类型: </td> <td> <label> ${bean.leixing} </label> </td> </tr> <tr> <td>4.活动时间: </td> <td> <label> ${bean.time} </label> </td> </tr> <tr> <td>5.活动地点: </td> <td> <label> ${bean.address} </label> </td> </tr> <tr> <td>6.活动对象: </td> <td> <label> ${bean.object} </label> </td> </tr> <tr> <td>7.活动内容: </td> <td> <label> ${bean.neirong} </label> </td> </tr> <tr> <td>8.活动日程安排: </td> <td> <label> ${bean.anpai} </label> </td> </tr> <tr> <td> <label> <form name="search1" method="post" action="delete.jsp"> <input type="submit" value="删除"> </form> <form name="search1" method="post" action="update.jsp"> <input type="submit" value="修改"> </form> </label> </td> </tr> </table> </body> </html>
liulan.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="bean.Bean" %> <%@ page import="dao.Dao" %> <%@ page import="util.Util" %> <%@ page import="java.sql.*" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>活动信息浏览</title> </head> <body> <div align="center"> <form name="form" method="post" action="Menu.jsp"> <input type="submit" value="返回主页"> </form> <table align="center" border="1" width="500"> <tr> <th>活动主题</th> <th>活动目的</th> <th>活动类型</th> <th>活动时间</th> <th>活动地点</th> <th>活动对象</th> <th>活动内容</th> <th>活动日程安排</th> </tr> <% String sql = "SELECT * FROM `shetuan`"; Connection conn = Util.getConn(); Statement state = null; ResultSet rs = null; Bean bean=null; try { state = conn.createStatement(); rs = state.executeQuery(sql); while (rs.next()) { String theme = rs.getString("theme"); String mudi = rs.getString("mudi"); String leixing = rs.getString("leixing"); String time = rs.getString("time"); String address = rs.getString("address"); String object = rs.getString("object"); String neirong = rs.getString("neirong"); String anpai = rs.getString("anpai"); out.println("<tr>"+"<td>"+theme+"</td>"); out.println("<td>"+mudi+"</td>"); out.println("<td>"+leixing+"</td>"); out.println("<td>"+time+"</td>"); out.println("<td>"+address+"</td>"); out.println("<td>"+object+"</td>"); out.println("<td>"+neirong+"</td>"); out.println("<td>"+anpai+"</td>"+"</tr>"); } } catch (Exception e) { e.printStackTrace(); } finally { Util.close(rs, state, conn); } %> <form name="liulan" method="post" action="delete.jsp"> <input type="submit" value="删除"> </form> <form name="liulan" method="post" action="update.jsp"> <input type="submit" value="修改"> </table> </div> </body> </html>
这里把查询分为两个界面,一个是查询界面另一个是结果显示。
至此,校园社团活动管理系统的增删改查以及浏览就完成了,代码里面也有一些注释,希望能够帮你更好的理解。
标签:String,期中考试,req,theme,getParameter,bean,社团活动,public,2021 From: https://www.cnblogs.com/zbw-m/p/16843715.html