點贊再看,養成習慣
程序幫QQ:1022287044mysql
用戶端
ajax
管理端
sql
1.添加問卷:數據庫
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { String questionTitle = request.getParameter("questionTitle"); //問卷標題 String qId = request.getParameter("qId"); //問卷id Integer ops =Integer.valueOf(request.getParameter("ops"));//具體幾組 String doType=request.getParameter("doType"); //操做類型 User user=(User)request.getSession().getAttribute(User.SESSIONNAME); SubjectService subjectService=new SubjectServiceImpl(); List<Subject> subjects=new ArrayList<>(); //問題列表 Question question=new Question(); question.setTitle(questionTitle); if("add".equalsIgnoreCase(doType)){ //若是修改添加id if(null!=user){//用戶對象不爲空,保存值 question.setUserId(user.getId()); } }else{ question.setId(Integer.valueOf(qId)); } String title=""; for(int i=0;i<ops;i++){ int number = Integer.parseInt(request.getParameter("number"+i)); String[] os = request.getParameterValues("options"+i); String[] osIds = request.getParameterValues("optionsIds"+i);//選項id title=request.getParameter("title"+i);//問題標題 String titleId=request.getParameter("titleId"+i);//問題id Subject subject = new Subject(); subject.setOrderId(i); subject.setTitle(title); subject.setNumber(number); if(null!=titleId&&!"".equalsIgnoreCase(titleId)){//設置問題id subject.setId(Integer.valueOf(titleId)); } for(int i1=0;i1<os.length;i1++){ Option op = new Option(); op.setContent(os[i1]); if("modify".equalsIgnoreCase(doType)){ //若是修改添加id op.setId(Integer.valueOf(osIds[i1])); } subject.getOptions().add(op); } try { long startTime=new Date().getTime(); subject.setStartTime(startTime); subject.setEndTime(startTime+1*24*60*60*1000); subject.setMaster(user); subjects.add(subject); } catch (ReTryException e) { HttpSession session = request.getSession(); session.setAttribute("subject", subject); session.setAttribute("message", e.getMessage()); response.sendRedirect(request.getContextPath() + "/m/add"); } catch (Exception ex) { throw new RuntimeException(ex); } } try{ if("add".equalsIgnoreCase(doType)){//添加 subjectService.add(question,subjects); }else{//修改 subjectService.modify(question,subjects); } }catch (Exception e){ e.printStackTrace(); } if(null!=user&&user.getRole().equalsIgnoreCase("user")){ response.sendRedirect(request.getContextPath() + "/list?action=myRelease");//用戶查看本身的發佈 }else{ response.sendRedirect(request.getContextPath() + "/list?action=index"); } }
2.修改問卷信息tomcat
public void doGet(HttpServletRequest request, HttpServletResponse response) { int id = Integer.parseInt(request.getParameter("id")); try { SubjectService subjectService = new SubjectServiceImpl(); QuestionService questionService = new QuestionServiceImpl(); SubjectQueryModel subjectQueryModel=new SubjectQueryModel(); subjectQueryModel.setQuestionId(id); List<Subject> subject = subjectService.getSubjects(subjectQueryModel);//問題列表 subjectQueryModel.setQuestionId(id); Question question=questionService.getQuestion(id); request.setAttribute("subjects", subject); request.setAttribute("ops", subject.size());//多少組 request.setAttribute("question", question); request.getRequestDispatcher("/WEB-INF/pages/modify.jsp") .forward(request, response); } catch (Exception e) { throw new RuntimeException(e); } }
3.問卷投票session
public void doGet(HttpServletRequest request, HttpServletResponse response) { try{ PrintWriter printWriter = response.getWriter(); int subjectId=Integer.parseInt(request.getParameter("subjectId")); String id = request.getParameter("options"); String[] ids = id.split(","); if(null==ids){ //未選中任何一項答案 printWriter.println(0); return; }else{ User user= (User)request.getSession().getAttribute(User.SESSIONNAME); //若是登陸用戶,判斷是否已經投過票 if(null!=user){ RecordQueryModel recordQueryModel=new RecordQueryModel(); recordQueryModel.getUser().setId(user.getId()); recordQueryModel.getSubject().setId(subjectId); List<Record> list=recordService.getVotes(recordQueryModel); if(list.size()>0){ //當前用戶已經投過票 printWriter.println(2); return; } } List<Record> records = new ArrayList<>(); for(int i=0;i<ids.length;i++){ Record record = new Record(); record.getSubject().setId(subjectId); record.getOption().setId(Integer.parseInt(ids[i])); if(null!=user){ record.getUser().setId(user.getId()); } records.add(record); } try { recordService.vote(records); //保存問卷信息 printWriter.println(1);//參與成功 return; } catch (ReTryException e) { e.printStackTrace(); printWriter.println(3);//參與成功 return; }catch(Exception ex){ ex.printStackTrace(); throw new RuntimeException(ex); } } } catch (Exception e){ e.printStackTrace(); } }
根據jdbc直連技術,編寫數據庫操做工具類,方便存儲數據,代碼以下:架構
public class DBUtils { String url = null; //鏈接地址 String username = null; //數據庫名 String password = null; //數據庫密碼 String driverClass = null; //鏈接驅動 private static DBUtils db = new DBUtils(); /**構建數據庫鏈接參數*/ private DBUtils() { try { url = "jdbc:mysql://localhost:3306/shopCartDb?useUnicode=true&characterEncoding=utf8"; username = "root"; password = "root123"; driverClass = "com.mysql.jdbc.Driver"; Class.forName(driverClass); } catch (Exception e) { e.printStackTrace(); } } /**構建數據庫鏈接對象*/ public Connection getConnection(){ Connection conn = null; try { conn = DriverManager.getConnection(url, username, password); } catch (Exception e) { e.printStackTrace(); } return conn; } public static DBUtils getInstance(){ return db; }}
4.投票可視化代碼mvc
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); try { int subjectId=Integer.parseInt(request.getParameter("subjectId")); //題目 RecordService recordService=new RecordServiceImpl(); List<Record> list=recordService.geyVotes(subjectId); if(list.size()>0){ session.setAttribute("list", list); session.setAttribute("stitle", list.get(0).getSubject().getTitle()); List<String> opDatas=new ArrayList<>(); Map<String,Integer> map=new HashMap<>(); for(Record r:list){ if(null!=r.getOption()){ if(!opDatas.contains(r.getOption().getContent())){ opDatas.add(r.getOption().getContent()); map.put(r.getOption().getContent(),0); } map.put(r.getOption().getContent(),map.get(r.getOption().getContent())+1); } } String opData=""; for(String o:opDatas){ opData=opData+"'"+o+"',"; } if(opData.length()>0){ opData=opData.substring(0,opData.length()-1); } session.setAttribute("opData", opData); //選項 String datavn=""; for (String key : map.keySet()) { datavn=datavn+"{ value: "+map.get(key)+", name: '"+key+"' },"; } if(datavn.length()>0){ datavn=datavn.substring(0,datavn.length()-1); } session.setAttribute("datavn", datavn); request.getRequestDispatcher("/WEB-INF/pages/view.jsp").forward(request, response); return; }else{ request.setAttribute("exception","暫無問卷結果!"); request.getRequestDispatcher("/WEB-INF/pages/500.jsp").forward(request, response); return; } } catch (ReTryException e) { request.getSession().setAttribute("message", e.getMessage()); response.sendRedirect(request.getContextPath()+"/m/view"); }catch(Exception ex){ ex.printStackTrace(); } }