31设计时间2011年12月12日——12月16日2设计目的JavaEE课程设计是对所学JavaEE与中间件课程的小结,是提高学生对所学知识综合应用能力的一种方式,是集中实践性环节之一。要求同学们对课程中所学习到的知识综合运用,开发有一定规模的JavaWeb程序。3设计任务设计生活质量衡量系统(1)衡量标准说明:消费有两种支出:吃饭支出和其它支出,如果在一段时间内,吃饭支出占总支出的比例超过50%,生活质量为贫困;如果在20-50%之间,为温饱;如果低于20%,生活质量为小康。(2)功能描述:用户以合法身份登录系统后,才能进行所有操作;用户可以添加、查看和删除支出情况(支出类型(有两种:吃饭支出和其它支出)、额度(人民币)、日期)。(3)添加支出情况(4)查看支出情况(5)删除支出情况(6)统计在某个时间段内,生活质量属于哪个层次4设计内容4.1设计题目生活质量衡量系统。44.1.1系统功能要求用只有拥有合法身份才能登录系统,用以合法身份登录后才能进行添加、查看和删除支出情况(支出类型(有两种:吃饭支出和其它支出)、额度(人民币)、日期)。4.1.2数据库存储要求数据的存储要求:吃饭支出其他支出,日期。4.1.3数据库的设计图1—1(数据库存储表)图1—2(数据库存储数据)54.1.4系统构造关系noyes图1—3(jsp页面构造)图1—4(Java类功能调用)登陆窗口密码查询插入删除计算操作界面Jsp页面传递参数调用servlet类参数插入方法servlet查询方法servlet删除方法servlet计算方法servlet插入方法dao类查询方法dao类删除方法dao类计算方法dao类64.2Jsp页面设计4.2.1登录界面图1—4(登录界面)代码如下:bodyformaction=loginmethod=postcenter欢迎登陆生活质量衡量系统!/centercenter用户名称:inputtype=textname=usernamevalue=brcenter用户密码:inputtype=passwordname=userpswvalue=br/centercenterinputtype=submitname=submitvalue=登录/centercenterinputtype=resetname=resetvalue=重写/center/form/body4.2.2登录成功界面图1—5(登录成功查询界面)bodyformaction=servlet/ListAllThingServletcenter登陆成功!点击获得详细的信息~~/centercenterinputtype=submitname=submitvalue=显示所有信息//center/form/body74.2.3查询结果界面图1—6(查询结果界面)4.3Java方法设计4.3.1Servlet类(1)密码验证的Servlet类的代码如下:publicclassLoginServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{Stringusername=(String)request.getParameter(username);Stringuserpsw=(String)request.getParameter(userpsw);System.out.println(username);Stringforward=;if(username.equals(123)&&userpsw.equals(123)){forward=/success.jsp;}else{8forward=/error.jsp;}(2)显示所有信息的servlet类代码设计如下:publicclassListAllThingServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{ThingDAOthingDAO=newThingDAO();Listlist=thingDAO.getAllThing();request.setAttribute(allthing,list);request.getRequestDispatcher(/showAllThing.jsp).forward(request,response);}}(3)控制增删改操作的servlet类代码设计如下:插入支出情况代码:publicclassSaveOutServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doublex=0.0;doubley=0.0;x=Double.parseDouble(request.getParameter(eating));y=Double.parseDouble(request.getParameter(others));9Stringtimes=(String)request.getParameter(times);ThingDAOthingDAO=newThingDAO();try{thingDAO.saveUser(x,y,times);}catch(Exceptione){e.printStackTrace();}Listlist=thingDAO.getAllThing();request.setAttribute(allthing,list);request.getRequestDispatcher(/showAllThing.jsp).forward(request,response);}}删除支出情况代码:publicclassDeleteOutServletextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doublex=0.0;x=Double.parseDouble(request.getParameter(eating));ThingDAOthingDAO=newThingDAO();try{thingDAO.deleteUser(x);10}catch(Exceptione){e.printStackTrace();}Listlist=thingDAO.getAllThing();request.setAttribute(allthing,list);request.getRequestDispatcher(/showAllThing.jsp).forward(request,response);}}衡量生活质量代码:publicclassCaculateextendsHttpServlet{publicvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{Stringtime1=request.getParameter(time1);Stringtime2=request.getParameter(time2);ThingDAOthingDAO=newThingDAO();try{Stringresulte=thingDAO.caculate(time1,time2);request.setAttribute(resulte1,resulte);request.getRequestDispatcher(/caculateresulte.jsp).forward(request,response);}catch(Exceptione){11e.printStackTrace();}}}4.3.2Dao类(1)显示所有支出情况的Dao类代码设计如下:publicclassThingDAO{publicListgetAllThing(){Connectioncon=DBConnection.getConnection();Listlist=newArrayList();try{Statementps=con.createStatement();//Statement(select*frommoneying);ResultSetrs=ps.executeQuery(select*frommoneying);while(rs.next()){Thingthing=newThing();thing.setEating(rs.getDouble(1));thing.setOthers(rs.getDouble(2));thing.setTimes(rs.getString(3));list.add(thing);}returnlist;}catch(SQLExceptione){e.printStackTrace();}finally{if(con!=null){try{con.close();12}catch(SQLExceptione){e.printStackTrace();}}}returnlist;}(2)添加,删除和统计生活质量的Dao类代码设计如下:publicbooleansaveUser(doublem,doublen,Stringq)throwsException{Connectioncon=DBConnection.getConnection();StringlistSQL=insertintomoneyingvalues(?,?,?);PreparedStateme