期中考试增删改查
青年志愿者服务网(20分)
1、项目需求:
为了适应社会主义市场经济发展的需要,推动青年志愿服务体系和多层次社会保障体系的建立和完善,促进青年健康成长,石家庄铁道大学急需建设青年志愿者服务网,推进石家庄铁道大学青年志愿者事业发展。
2.系统要求与功能设计
2.1 页面功能要求
(1)能够在Tomcat服务器中正确部署,并通过浏览器查看;(1分)
(2)网站页面整体风格统一;

图1 青年志愿者信息管理系统功能结构图
(3)主页面:要求显示志愿者登记、修改志愿者信息、删除志愿者信息、查询志愿者信息,志愿者信息浏览五个子菜单。(1分)
(4)志愿者登记页面:(5分)
①完成添加志愿者个人信息登记,基本信息如下表所示

②性别要求采用单选框形式,选项包括“男”、“女”两项;
③政治面目要求采用下拉框选择,选项内容包括“群众”、“共青团员”、“中共党员”三项。
④服务类别采用多选框形式选择,最多选择四项服务类别。
⑤点击“提交”按钮,保存成功则跳转到志愿者基本信息浏览界面,新录入的信息置顶显示。失败则提示错误信息,返回当前页面
评分标准:
①完成志愿者登记页面(未完成0分,完成1分)
②保存志愿者信息入库(未完成0分,完成1分)
③性别单选框录入(未完成0分,完成0.5分)
④ 政治面目下拉框录入(未完成0分,完成0.5分)
⑤服务类别多选框录入功能(未完成0分,完成0.5分)
⑥服务类别限制最多四项选项功能;(未完成0分,完成0.5分)
⑦提交后页面跳转功能;(未完成0分,完成1分)
(5)修改志愿者个人信息页面:
输入志愿者姓名,显示其余信息,可对性别、政治面目、服务类别进行修改。(性别、政治面目、服务类别必须符合录入要求);如果该名志愿者信息数据库不存在,则提示“该志愿者信息不存在”。(3分)
评分标准:
①完成修改志愿者个人信息页面(未完成0分,完成0.5分)
② 实现数据库中信息更新(未完成0分,完成1分)
③修改信息判断是否符合要求。(未完成0分,完成0.5分)
④输入姓名,显示其余信息,若该信息不存在,提示错误信息;(未完成0分,完成1分)
(6)删除志愿者信息页面:录入志愿者姓名,显示详细信息后,点击“删除”按钮,弹出提示框,提示“是否确认删除该志愿者信息”,确认后删除该信息。(2分)
评分标准:
①输入志愿者姓名可显示其余信息。(未完成0分,完成0.5分)
②提示确认信息。(未完成0分,完成0.5分)
③对应删除数据库中信息(未完成0分,完成1分)
(7)浏览志愿者信息页面:(2分)
以列表形式显示志愿者基本信息,结果列表中显示姓名、性别,民族、政治面目基本信息,点击姓名,可以跳转到个人详细信息。
①实现以列表形式显示志愿者基本信息,结果列表中显示姓名、性别,民族、政治面目基本信息(未完成0分,完成1分)
②实现跳转个人详细信息页面。(未完成0分,完成1分)
(8)查询志愿者信息页面:(3分)
要求可以根据志愿者姓名、性别、民族、政治面目、服务类别、注册时间六种条件实现模糊查询,输出结果以列表形式显示,显示姓名、性别,民族、政治面目基本信息,点击列表中的姓名,跳转到志愿者个人详细信息页面。
评分标准:
①缺少一种查询条件扣除0.5分。(未完成0分,完成3分)
2.2 功能要求
(1)设计出合理的数据库和数据表,要求使用mysql、sqlserver、oracle三种数据库中一种(1分)
(2)使用Serverlet实现页面交互(1分)。
(3)使用Java Bean封装数据库连接操作(1分。)
下面是我的代码
1 package com.bean; 2 3 public class Bean { 4 5 private String name; 6 private String sex; 7 private String mianmu; 8 private String minzu; 9 private String time; 10 private String age; 11 private String fuwu; 12 13 14 15 public Bean() { 16 super(); 17 } 18 19 public Bean(String name, String sex, String mianmu, String minzu, String time, String age, String fuwu) { 20 super(); 21 this.name = name; 22 this.sex = sex; 23 this.mianmu = mianmu; 24 this.minzu = minzu; 25 this.time = time; 26 this.age = age; 27 this.fuwu = fuwu; 28 } 29 30 31 public String getName() { 32 return name; 33 } 34 35 public void setName(String name) { 36 this.name = name; 37 } 38 39 public String getSex() { 40 return sex; 41 } 42 43 public void setSex(String sex) { 44 this.sex = sex; 45 } 46 47 public String getMianmu() { 48 return mianmu; 49 } 50 51 public void setMianmu(String mianmu) { 52 this.mianmu = mianmu; 53 } 54 55 public String getMinzu() { 56 return minzu; 57 } 58 59 public void setMinzu(String minzu) { 60 this.minzu = minzu; 61 } 62 63 public String getTime() { 64 return time; 65 } 66 67 public void setTime(String time) { 68 this.time = time; 69 } 70 71 public String getAge() { 72 return age; 73 } 74 75 public void setAge(String age) { 76 this.age = age; 77 } 78 79 public String getFuwu() { 80 return fuwu; 81 } 82 83 public void setFuwu(String fuwu) { 84 this.fuwu = fuwu; 85 } 86 87 @Override 88 public String toString() { 89 return "Bean [name=" + name + ", sex=" + sex + ", mianmu=" + mianmu + ", minzu=" + minzu + ", time=" + time 90 + ", age=" + age + ", fuwu=" + fuwu + "]"; 91 } 92 93 94 }
1 package com.dao; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.Statement; 6 import java.util.ArrayList; 7 import java.util.List; 8 import java.sql.ResultSet; 9 import java.sql.SQLException; 10 11 import com.db.DB; 12 import com.servlet.selectNameServlet; 13 import com.servlet.updateServlet; 14 import com.db.DB; 15 import com.bean.Bean; 16 17 public class Dao { 18 19 //增加 20 @SuppressWarnings("static-access") 21 22 public boolean add(Bean bean) { 23 String tablename = "test"; 24 DB db=new DB(); 25 Connection con = db.getCon(); 26 27 try { 28 String sql="insert into "+tablename+"(name,sex,mianmu,minzu,time,age,fuwu) values ('"+bean.getName()+"','"+bean.getSex()+"','"+bean.getMianmu()+"','"+bean.getMinzu()+"','"+bean.getTime()+"','"+bean.getAge()+"','"+bean.getFuwu()+"')"; 29 Statement stm = con.createStatement(); 30 System.out.println(sql); 31 stm.execute(sql); 32 db.close(stm, con); 33 }catch(Exception e) { 34 e.printStackTrace(); 35 System.out.println("add false"); 36 return false; 37 } 38 System.out.println("add true"); 39 return true; 40 } 41 42 43 //根据名称进行查询 44 @SuppressWarnings("static-access") 45 public static Bean selectName(String name) { 46 String tablename = "test"; 47 System.out.println("select bean where name = "+name); 48 Bean bean=null; 49 DB db=new DB(); 50 Connection con = db.getCon(); 51 try { 52 Statement stm = con.createStatement(); 53 //閿熸枻鎷烽敓鏂ゆ嫹value1=name閿熸枻鎷烽敓鏂ゆ嫹閿熸嵎锝忔嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓琛楄鎷烽敓鏂ゆ嫹閿熸枻鎷� 54 ResultSet rs = stm.executeQuery("select * from "+tablename+" where name='" + name + "'"); 55 /* 56 * 閿熸枻鎷烽敓鏂ゆ嫹value1=name閿熸枻鎷烽敓鏂ゆ嫹閿熸嵎锝忔嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹value3閿熸枻鎷烽敓琛楄鎷烽敓鏂ゆ嫹閿熸枻鎷� 57 * ResultSet rs =stm.executeQuery("select value3 from "+tablename+" where value2="+i); 58 */ 59 if(rs.next()) 60 { 61 bean = new Bean(); 62 System.out.println("select the bean from mysql"); 63 bean.setName(rs.getString("name")); 64 bean.setSex(rs.getString("sex")); 65 bean.setMianmu(rs.getString("mianmu")); 66 bean.setMinzu(rs.getString("minzu")); 67 bean.setTime(rs.getString("time")); 68 bean.setAge(rs.getString("age")); 69 bean.setFuwu(rs.getString("fuwu")); 70 //bean.setValue2(Integer.parseInt(rs.getString("teacher"))); 71 //bean.setValue3(Boolean.parseBoolean(rs.getString("address"))); 72 System.out.println("name of the bean is "+rs.getString("name")); 73 } 74 db.close(rs,stm, con); 75 }catch(Exception e) { 76 e.printStackTrace(); 77 } 78 return bean; 79 } 80 81 //根据性别进行查询 82 @SuppressWarnings("static-access") 83 public static Bean selectSex(String sex) { 84 String tablename = "test"; 85 System.out.println("select bean where sex = "+sex); 86 Bean bean=null; 87 DB db=new DB(); 88 Connection con = db.getCon(); 89 try { 90 Statement stm = con.createStatement(); 91 //閿熸枻鎷烽敓鏂ゆ嫹value1=name閿熸枻鎷烽敓鏂ゆ嫹閿熸嵎锝忔嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓琛楄鎷烽敓鏂ゆ嫹閿熸枻鎷� 92 ResultSet rs = stm.executeQuery("select * from "+tablename+" where sex='" + sex + "'"); 93 /* 94 * 閿熸枻鎷烽敓鏂ゆ嫹value1=name閿熸枻鎷烽敓鏂ゆ嫹閿熸嵎锝忔嫹閿熸枻鎷烽敓鏂ゆ嫹閿熸枻鎷烽敓鏂ゆ嫹value3閿熸枻鎷烽敓琛楄鎷烽敓鏂ゆ嫹閿熸枻鎷� 95 * ResultSet rs =stm.executeQuery("select value3 from "+tablename+" where value2="+i); 96 */ 97 if(rs.next()) 98 { 99 bean = new Bean(); 100 System.out.println("select the bean from mysql"); 101 bean.setName(rs.getString("name")); 102 bean.setSex(rs.getString("sex")); 103 bean.setMianmu(rs.getString("mianmu")); 104 bean.setMinzu(rs.getString("minzu")); 105 bean.setTime(rs.getString("time")); 106 bean.setAge(rs.getString("age")); 107 bean.setFuwu(rs.getString("fuwu")); 108 //bean.setValue2(Integer.parseInt(rs.getString("teacher"))); 109 //bean.setValue3(Boolean.parseBoolean(rs.getString("address"))); 110 System.out.println("sex of the bean is "+rs.getString("sex")); 111 } 112 db.close(rs,stm, con); 113 }catch(Exception e) { 114 e.printStackTrace(); 115 } 116 return bean; 117 } 118 119 120 121 122 //删除 123 @SuppressWarnings("static-access") 124 public static boolean delete(String name) 125 { 126 //String tablename ="test"; 127 DB db=new DB(); 128 Connection con = db.getCon(); 129 //Connection con=null; 130 PreparedStatement stm=null; 131 try { 132 con=DB.getCon(); 133 String sql="delete from test where name='"+name+"'"; 134 //String sql="delete from test where name='"+bean.getName()+"'"; 135 System.out.println(sql); 136 stm=con.prepareStatement(sql); 137 stm.executeUpdate(); 138 return true; 139 } 140 catch(SQLException e) 141 { 142 e.printStackTrace(); 143 } 144 finally { 145 db.close(stm, con); 146 } 147 return false; 148 149 } 150 //修改 151 @SuppressWarnings("static-access") 152 public boolean update(Bean bean) { 153 Connection con=null; 154 PreparedStatement stm=null; 155 String tablename="test"; 156 DB db=new DB(); 157 try { 158 con=DB.getCon(); 159 String sql="update "+tablename+" set mianmu=?,sex=? ,minzu=?,time=?,age=?,fuwu=? where name=?"; 160 //String sql="update "+tablename+"set teacher=?,classroom=? where name=?"; 161 stm=con.prepareStatement(sql); 162 stm.setString(7,bean.getName()); 163 stm.setString(2,bean.getSex()); 164 stm.setString(1,bean.getMianmu()); 165 stm.setString(3,bean.getMinzu()); 166 stm.setString(4,bean.getTime()); 167 stm.setString(5,bean.getAge()); 168 stm.setString(6,bean.getFuwu()); 169 stm.executeUpdate(); 170 } 171 catch(SQLException e) 172 { 173 e.printStackTrace(); 174 return false; 175 } 176 finally { 177 db.close(stm, con); 178 return true; 179 } 180 } 181 //根据名字进行查找 182 public boolean findName(String name){ 183 boolean flag=false;//鎵句笉鍒� 184 Connection con=null; 185 PreparedStatement psts=null; 186 ResultSet rs=null; 187 String tablename="test"; 188 DB db=new DB(); 189 try { 190 con=db.getCon(); 191 String sql="select * from"+tablename+" where name=?"; 192 psts=con.prepareStatement(sql); 193 psts.setString(1, name); 194 rs=psts.executeQuery(); 195 while(rs.next()){ 196 flag=true; 197 } 198 } catch (SQLException e) { 199 e.printStackTrace(); 200 } 201 202 return flag; 203 } 204 205 // 206 @SuppressWarnings("static-access") 207 public ArrayList<Bean> selectList(){ 208 Connection con=null; 209 PreparedStatement stm=null; 210 ResultSet rs=null; 211 ArrayList<Bean> listbean=new ArrayList<Bean>(); 212 String tablename="test"; 213 DB db=new DB(); 214 try { 215 con=db.getCon(); 216 String sql="select * from "+tablename; 217 stm=con.prepareStatement(sql); 218 rs=stm.executeQuery(); 219 while(rs.next()){ 220 221 String name=rs.getString("name"); 222 String sex=rs.getString("sex"); 223 String mianmu=rs.getString("mianmu"); 224 String minzu=rs.getString("minzu"); 225 String time=rs.getString("time"); 226 String age=rs.getString("age"); 227 String fuwu=rs.getString("fuwu"); 228 Bean bean=new Bean(name,sex,mianmu,minzu,time,age,fuwu); 229 listbean.add(bean); 230 231 } 232 } 233 catch(SQLException e) 234 { 235 e.printStackTrace(); 236 } 237 finally 238 { 239 db.close(stm, con); 240 } 241 return listbean; 242 } 243 244 245 public static List<Bean> loadview(String sql) { 246 System.out.println("loadview??sql:"+sql); 247 String sql1="select * from test "+ sql; 248 System.out.println("loadview??sql1:"+sql1); 249 List<Bean> list =new ArrayList<Bean>(); 250 Connection conn = DB.getCon(); 251 Statement state = null; 252 ResultSet rs = null; 253 Bean yi=null; 254 try 255 { 256 state = conn.createStatement(); 257 rs = state.executeQuery(sql1); 258 while(rs.next()) 259 { 260 261 String name=rs.getString("name"); 262 String sex=rs.getString("sex"); 263 String mianmu=rs.getString("mianmu"); 264 String minzu=rs.getString("minzu"); 265 String time=rs.getString("time"); 266 String age=rs.getString("age"); 267 String fuwu=rs.getString("fuwu"); 268 269 System.out.println("name"+name); 270 271 yi=new Bean(name,sex,mianmu,minzu,time,age,fuwu); 272 list.add(yi); 273 System.out.println("name1"+yi.getName()); 274 } 275 276 DB.close(rs, state, conn); 277 } catch (SQLException e) { 278 // TODO �Զ����ɵ� catch �� 279 e.printStackTrace(); 280 } 281 282 return list; 283 } 284 } 285 286 287 288 289 // 290 // public Bean login(String name,String password) { 291 // String tablename = null; 292 // DB db=new DB(); 293 // Connection con = db.getCon(); 294 // Bean bean = null; 295 // try { 296 // Statement stm = con.createStatement(); 297 // ResultSet rs = stm.executeQuery("select * from "+tablename+" where value1='" + name + "'"); 298 // if(rs.next()) { 299 // if (rs.getString("password").equals(password)) { 300 // 301 // } 302 // } 303 // }catch(Exception e) { 304 // 305 // } 306 // } 307
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 com.bean.Bean; import com.dao.Dao; /** * Servlet implementation class addServlet */ @WebServlet("/addServlet") public class addServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final String String = null; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("UTF-8"); Bean bean = new Bean(); bean.setName(request.getParameter("name")); bean.setSex(request.getParameter("sex")); bean.setMianmu(request.getParameter("mianmu")); bean.setMinzu(request.getParameter("minzu")); bean.setTime(request.getParameter("time")); bean.setAge(request.getParameter("age")); String []comments=request.getParameterValues("fuwu"); String coment = String.join(",",comments); bean.setFuwu(coment); System.out.println(coment); Dao dao = new Dao(); if(dao.add(bean)) { request.setAttribute("message", "添加成功"); request.getRequestDispatcher("add.jsp").forward(request,response); //response.sendRedirect("index.jsp"); } else { request.setAttribute("message","添加失败"); request.getRequestDispatcher("add.jsp").forward(request,response); //response.sendRedirect("add.jsp"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } 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 com.bean.Bean; import com.dao.Dao; /** * Servlet implementation class delete1servlet */ @WebServlet("/delete1servlet") public class delete1servlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); Bean bean=new Bean(); Bean beanform=new Bean(); Dao dao=new Dao(); bean = dao.selectName(request.getParameter("delete")); System.out.println(request.getParameter("delete")); if(bean!=null) { request.setAttribute("Bean",bean); request.getRequestDispatcher("delete_show.jsp").forward(request,response); } else { request.setAttribute("message", "该志愿者信息不存在"); request.getRequestDispatcher("delete.jsp").forward(request,response); //response.sendRedirect("index.jsp"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } package com.servlet; import java.io.IOException; import java.io.PrintWriter; 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.bean.Bean; import com.dao.Dao; /** * Servlet implementation class deleteServlet */ @WebServlet("/deleteServlet") public class deleteServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); Dao dao=new Dao(); if(dao.delete(request.getParameter("deletename")) ) { request.setAttribute("message","删除成功"); request.getRequestDispatcher("delete.jsp").forward(request,response);//转发到jsp里面 } else { request.setAttribute("message", "删除失败,请重新进行删除"); request.getRequestDispatcher("delete.jsp").forward(request,response); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } } package com.servlet; import java.io.IOException; import java.util.ArrayList; 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 javax.servlet.http.HttpSession; import com.bean.Bean; import com.dao.Dao; /** * Servlet implementation class MoHuServlet */ @WebServlet("/MoHuServlet") public class MoHuServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public MoHuServlet() { 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 request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); Dao yidao=new Dao(); List<Bean>list = new ArrayList<Bean>(); String p[] = new String[10];//获得条件 String s[] = new String[10];//获得内容 String g[] = new String[10];//获得关系 String f[] = new String[10];//获得精确或者模糊 String number = null; //String number1 = null; number=request.getParameter("number1"); //number1=request.getParameter("number2"); int n = Integer.parseInt(number);//接收条件的个数 System.out.println("条件数"+n); //int n1 = Integer.parseInt(number1);//���յĹ�ϵ�ĸ��� for(int i=1;i<=n;i++) { p[i]= (String) request.getParameter("s"+i); s[i]=(String)request.getParameter("shuru"+i); f[i]=(String)request.getParameter("c"+i); System.out.println("精确还是模糊"+f[i]); System.out.println("条件"+p[i]); System.out.println("输入"+s[i]); if(p[i].equals("名字")){ p[i] = "name"; }else if(p[i].equals("性别")){ p[i] = "sex"; }else if(p[i].equals("民族")){ p[i] = "minzu"; }else if(p[i].equals("注册日期")){ p[i] = "time"; }else if(p[i].equals("年龄")){ p[i] = "age"; } else if(p[i].equals("面目")) { p[i]="mianmu"; } else if(p[i].equals("服务")) { p[i]="fuwu"; } } for(int i=1;i<n;i++) { g[i]=(String) request.getParameter("g"+i); if(g[i].equals("且")) { g[i]="and"; }else if(g[i].equals("或")) { g[i]="or"; } System.out.println("且或"+g[i]); } String sql="where "; for(int i=1;i<n;i++) { if(s[i].equals("")) { continue; } if(f[i].equals("精确")) { sql=sql+p[i]+"='"+s[i]+"'"+" "+g[i]+" "; }else if(f[i].equals("模糊")) { sql=sql+p[i]+" "+"LIKE"+" "+"'%"+s[i]+"%'"+" "+g[i]+" "; } } if(f[n].equals("精确")) { sql = sql + p[n]+"="+"'"+s[n]+"'"; }else { sql = sql + p[n]+" "+"LIKE"+" "+"'%"+s[n]+"%'"; } // } if(n==1&&s[1].equals("")) { System.out.println("什么也没传!"); sql=""; } list = yidao.loadview(sql); if(list.size()!=0) { request.setAttribute("course11",list); request.setAttribute("message", "查询成功"); System.out.print("查询 成功"); request.getRequestDispatcher("show.jsp").forward(request,response); } else { request.setAttribute("message", "查询失败"); request.getRequestDispatcher("jingquemohuchaxun.jsp").forward(request,response); } // HttpSession session = request.getSession(); // session.setAttribute("tiaojian", list); //// int servletid=1; //// request.setAttribute("list", list); //// request.setAttribute("servletid", servletid); // try { //// request.getRequestDispatcher("/admin/123.jsp").forward(request, response); // response.sendRedirect(request.getContextPath()+"/show.jsp"); // } catch (IOException e) { // // TODO �Զ����ɵ� catch �� // e.printStackTrace(); // } } /** * @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); } } 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 com.bean.Bean; import com.dao.Dao; /** * Servlet implementation class selectNameServlet */ @WebServlet("/selectNameServlet") public class selectNameServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); Bean bean=new Bean(); bean.setName(request.getParameter("selectname")); Dao dao=new Dao(); bean = dao.selectName(bean.getName()); if(bean!=null) { request.setAttribute("Bean",bean); request.getRequestDispatcher("select_show.jsp").forward(request,response); } } /** * @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); } } 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 com.bean.Bean; import com.dao.Dao; @WebServlet("/selectsexServlet") public class selectsexServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); Bean bean=new Bean(); bean.setSex(request.getParameter("selectsex")); Dao dao=new Dao(); bean = dao.selectSex(bean.getSex()); if(bean!=null) { request.setAttribute("Bean",bean); request.getRequestDispatcher("select_show.jsp").forward(request,response); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } 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 com.bean.Bean; import com.dao.Dao; /** * Servlet implementation class selectNameServlet */ @WebServlet("/Servlet") public class Servlet extends HttpServlet { private static final long serialVersionUID = 1L; Dao dao=new Dao(); protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Bean bean=new Bean(); String name = request.getParameter("name"); System.out.println(name+"哈哈哈哈哈哈哈哈哈哈哈哈dgywegdyuwedyugwedye"); bean = dao.selectName(name); // System.out.println(bean.getMinzu()); if(bean!=null) { request.setAttribute("Bean",bean); request.getRequestDispatcher("select_show.jsp").forward(request,response); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } package com.servlet; import java.io.IOException; import java.util.ArrayList; 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.bean.Bean; import com.dao.Dao; /** * Servlet implementation class showAll */ @WebServlet("/showAllServlet") public class showAllServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); Dao dao=new Dao(); ArrayList<Bean> listbean=dao.selectList(); //request.getRequestDispatcher(); request.setAttribute("listbean", listbean); request.getRequestDispatcher("showall.jsp").forward(request,response); } /** * @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); } } 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 com.bean.Bean; import com.dao.Dao; /** * Servlet implementation class updatenewServlet */ @WebServlet("/updatenewServlet") public class updatenewServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //response.getWriter().append("Served at: ").append(request.getContextPath()); request.setCharacterEncoding("UTF-8"); Bean beannew=new Bean(); Dao dao=new Dao(); beannew.setName(request.getParameter("updatename")); beannew.setSex(request.getParameter("updatesex")); beannew.setMianmu(request.getParameter("updatemianmu")); beannew.setMinzu(request.getParameter("updateminzu")); beannew.setTime(request.getParameter("updatetime")); beannew.setAge(request.getParameter("updateage")); beannew.setFuwu(request.getParameter("updatefuwu")); //System.out.println("���ܳɹ�"); //System.out.println(request.getParameter("updatename")); //bean.setValue2(Integer.parseInt(request.getParameter("value2"))); //bean.setValue3(Boolean.parseBoolean(request.getParameter("value3"))); if(dao.update(beannew)) { //request.setAttribute("message", "添加成功"); response.sendRedirect("index.jsp"); } else { //request.setAttribute("message","添加失败"); response.sendRedirect("update_show.jsp"); } } /** * @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); } } package com.servlet; import java.io.IOException; import java.io.PrintWriter; 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.bean.Bean; import com.dao.Dao; /** * Servlet implementation class updateServlet */ @WebServlet("/updateServlet") public class updateServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); Bean bean=new Bean(); Bean beanform=new Bean(); Dao dao=new Dao(); bean = dao.selectName(request.getParameter("selectname")); System.out.println(request.getParameter("selectname")); if(bean!=null) { request.setAttribute("Bean",bean); request.getRequestDispatcher("update_show.jsp").forward(request,response); } else { request.setAttribute("message", "该志愿者信息不存在"); request.getRequestDispatcher("update.jsp").forward(request,response); //response.sendRedirect("index.jsp"); } // else if(bean==null) { // request.setAttribute("Bean",bean); // request.getRequestDispatcher("delete.jsp").forward(request,response); // return ; // } // beanform.setName(request.getParameter("updatename")); // beanform.setTeacher(request.getParameter("updateteacher")); // beanform.setClassroom(request.getParameter("updateclassroom")); // //bean.setValue2(Integer.parseInt(request.getParameter("value2"))); // //bean.setValue3(Boolean.parseBoolean(request.getParameter("value3"))); // if(dao.update(beanform)) { // //request.setAttribute("message", "添加成功"); // response.sendRedirect("index.jsp"); // } // else { // //request.setAttribute("message","添加失败"); // response.sendRedirect("update.jsp"); // } } }
package com.db; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import org.junit.Test; public class DB { private static String mysqlname = "test";//���� private static Connection con; private static Statement stm; private static ResultSet rs; private static String coursename = "com.mysql.jdbc.Driver"; private static String url = "jdbc:mysql://127.0.0.1:3306/"+mysqlname+"?useUnicode=true&characterEncoding=GB18030&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true"; public static Connection getCon() { try { Class.forName(coursename); System.out.println("ע�������ɹ�"); }catch(ClassNotFoundException e) { e.printStackTrace(); } try { con = DriverManager.getConnection(url,"root","123456"); System.out.println("�������ӳɹ�"); }catch(Exception e){ e.printStackTrace(); con = null; } return con; } public static void close(Statement stm,Connection connection) { if(stm!=null) { try { stm.close(); }catch(SQLException e) { e.printStackTrace(); } } if(connection!=null) { try { connection.close(); }catch(SQLException e) { e.printStackTrace(); } } } //关闭 public static void close(ResultSet rs,Statement stm,Connection connection) { if(rs!=null) { try { rs.close(); }catch(SQLException e) { e.printStackTrace(); } } if(stm!=null) { try { stm.close(); }catch(SQLException e) { e.printStackTrace(); } } if(connection!=null) { try { connection.close(); }catch(SQLException e) { e.printStackTrace(); } } } public static void main(String[] args) { getCon(); } }
还有web的代码主要是界面这次的考试让我学会了很多考试的时候没有把全部的都实现但是,课下的时候吧所有的功能自己补全实现

浙公网安备 33010602011771号