1、 软 件 学 院 课程设计汇报书 课程名称 JavaEE 课程设计 设计题目 个人财务系统 专业班级 软件工程 学 号 xxxxxxxxx07 姓 名 xxxxx 指导老师 姜彦吉 年 12 月
2、 1 设计时间 12月12日——12月16日 2 设计目标 JavaEE课程设计是对所学JavaEE和中间件课程小结,是提升学生对所学知识综合应用能力一个方法,是集中实践性步骤之一。要求同学们对课程中所学习到知识综合利用,开发有一定规模Java Web程序。 3设计任务 设计个人帐务管理系统,要求用户以正当身份登录后能够对系统进行操作,用户能够查看,添加,删除和计算某段时间内帐务收入和支出信息等。帐务信息包含收入和支出两种,还有日期和备注。 4 设计内容 4.1 设计题目 个人财务管理系统 4.1.1系统功效要求 用只有拥
3、有正当身份才能登录系统,用以正当身份登录后能够产看帐务信息、添加帐务信息、删除帐务信息、分别统计某个时间段内收入和支出总额。 4.1.2 数据库存放要求 数据存放要求:收入数额,支出数额,备注,日期 4.1.3数据库设计 表—1(数据存放要求) 列名称 数据类型 长度 id bigint 8 incomemoney money 8 costmoney money 8 record varchar 50 time varchar 8 图—1(数据库存放数据) 4.1.4系统结构关系 登陆窗口
4、 no 密码 yes 操作界面 查询 保留 计算帐目 删除 图—2(jsp页面结构) Jsp页面传输参数 调用servlet类 参数 计算方法servlet 删除方法servlet 查询方法servlet 保留方法servlet 删除方法dao类 查询方法dao类 保留方法dao类 计算方法dao类 图—3(Java类功效调用) 4.2 Jsp 页面设
5、计 4.2.1登录界面 图—4(登录界面) 代码以下:
4.2.2登录成功界面 图—5(登录成功查询界面) 代码以下: <% String username=(String)session.getAttribute("name"); if(username!=null){%>7、 %>
11、 4.3.1Servlet类 控制增删改操作servlet类代码设计以下: public class AccountController extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } protected void doPost(HttpServletRequest r
12、equest, HttpServletResponse response) throws ServletException, IOException { int arg=Integer.parseInt(request.getParameter("arg")); switch(arg) { case 1:this.findAllAccount(request,response); break; case 2:
13、this.saveAllAccount(request,response); break; case 3:this.deleteSomeAccount(request,response); break; case 4:this.caculateAccount(request,response); break; } } protected void caculateAccount(HttpServlet
14、Request request, HttpServletResponse response) throws ServletException, IOException { int a=0; int b=0; a=Integer.parseInt(request.getParameter("id")); b=Integer.parseInt(request.getParameter("id")); AccountDao accountDao=new AccountDao();
15、 List list2=accountDao.caculateAccount(a,b); request.setAttribute("result",list2); RequestDispatcher rdt1=request.getRequestDispatcher("/Result1.jsp"); rdt1.forward(request, response); } public void findAllAccount(HttpServletRequest request, HttpServletResponse response)
16、 throws ServletException, IOException { AccountDao accountDao=new AccountDao(); List list=accountDao.findAllAccount(); request.setAttribute("accounts", list); RequestDispatcher rdt=request.getRequestDisp
17、atcher("/Result.jsp"); rdt.forward(request, response); } public void saveAllAccount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("gbk")
18、 double incomemoney=0.0; double costmoney=0.0; incomemoney=Double.parseDouble(request.getParameter("incomemoney")); costmoney=Double.parseDouble(request.getParameter("costmoney")); String record=(String)request.getParameter("record"); String time=(String)r
19、equest.getParameter("time"); MyAccount myAccount=new MyAccount(); myAccount.setIncomemoney(incomemoney); myAccount.setCostmoney(costmoney); myAccount.setRecord(record); myAccount.setTime(time); AccountDao accountDao=new AccountDao(); try { a
20、ccountDao.saveAllAccount(myAccount); } catch(Exception e) { e.printStackTrace(); } this.findAllAccount(request,response); } public void deleteSomeAccount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOExcep
21、tion { request.setCharacterEncoding("gbk"); String time=request.getParameter("time"); MyAccount myAccount=new MyAccount(); myAccount.setTime(time); AccountDao accountDao=new AccountDao(); try {
22、accountDao.deleteSomeAccount(myAccount); } catch(Exception e) { e.printStackTrace(); } this.findAllAccount(request, response); } } 密码验证Servlet类代码以下: public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest req, HttpServletResponse
23、resp) throws ServletException, IOException { doGet(req,resp); } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username=req.getParameter("username"); String password=req.getParameter("passw
24、ord"); User user=new User(); user.setUsername(username); user.setPassword(password); HttpSession session=req.getSession(); String forward=""; if(UserService.CheckLogin(user)) { forward="/Search.jsp"; session.setAttribute
25、"name", username); } else { forward="/error.jsp"; } RequestDispatcher rd=req.getRequestDispatcher(forward); rd.forward(req,resp); } } 4.3.2Dao类 密码验证Dao类代码设计以下: public class UserService { public static boolean CheckLogin(Us
26、er user) { if(user.getUsername().equals("lb")&&user.getPassword().equals("123")) { return true; } return false; } } 帐务添加查询计算Dao类代码设计以下: public class AccountDao { private Connection conn; public List findAllAccount() { conn=DBCon.getConn
27、ection(); String listSQL="select * from myaccount"; List list=new ArrayList(); try{ PreparedStatement psmt=conn.prepareStatement(listSQL); ResultSet rs=psmt.executeQuery(); while(rs.next()) { MyAccount account=new MyAccount();
28、 account.setId(rs.getInt(1)); account.setIncomemoney(rs.getDouble(2)); account.setCostmoney(rs.getDouble(3)); account.setRecord(rs.getString(4)); account.setTime(rs.getString(5)); list.add(account); } mit();
29、 return list; } catch(Exception e){ e.printStackTrace(); } finally { if(conn!=null) { try{ conn.close(); } catch(SQLException e){ e.printStackTrace(); } } } return list;
30、 } public boolean saveAllAccount(MyAccount account)throws Exception { conn=DBCon.getConnection(); String listSQL="insert into myaccount values(?,?,?,?)"; PreparedStatement psmt=conn.prepareStatement(listSQL); try{
31、 psmt.setDouble(1, account.getIncomemoney()); psmt.setDouble(2, account.getCostmoney()); psmt.setString(3, account.getRecord()); psmt.setString(4, account.getTime()); psmt.executeUpdate(); mit(); return true; } catch(SQLException e){ conn.r
32、ollback(); e.printStackTrace(); } return false; } public boolean deleteSomeAccount(MyAccount account)throws Exception { conn=DBCon.getConnection(); String listSQL="delete from myaccount where time=? "; PreparedStatement psmt=con
33、n.prepareStatement(listSQL); try { psmt.setString(1, account.getTime()); psmt.executeUpdate(); mit(); return true; } catch(SQLException e) { conn.rollback(); e.printStackTrace(); } return false; } public List caculateAccount(int
34、 x,int y) { conn=DBCon.getConnection(); double incometotal=0.0; double costtotal=0.0; int i=0; List list2=new ArrayList(); try{ MyAccount account=new MyAccount(); for( i=x;i<=y;i++) { String listSQL1="selct incomemone
35、y from myaccount where id=i"; PreparedStatement psmt1=conn.prepareStatement(listSQL1); ResultSet rs1=psmt1.executeQuery(); psmt1.execute(); incometotal=incometotal+rs1.getDouble(2); String listSQL2="select costmoney from myaccount where id=i"; PreparedStatemen
36、t psmt2=conn.prepareStatement(listSQL2); ResultSet rs2=psmt2.executeQuery(); psmt2.execute(); costtotal=costtotal+rs2.getDouble(3); } mit(); account.setIncomemoney(incometotal); account.setCostmoney(costtotal); list2.add(account); return list2;
37、 } catch(SQLException e) { e.printStackTrace(); } return list2; } } 4.3.3数据库连接类 数据库连接代码以下: public class DBCon { public static Connection getConnection() { String url="jdbc:microsoft:sqlserver://localhost:1433;databaseName=myaccountbase"; String user="sa";
38、 String psw="sa"; Connection conn=null; try{ Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); } catch(ClassNotFoundException e){ e.printStackTrace(); } try{ conn=DriverManager.getConnection(url, user, psw); conn.setAutoCommit(false); return conn;
39、 } catch(SQLException e){ e.printStackTrace(); } return null;} } 5 总结和展望 经过此次课程设计自己JavaWeb编程加深了了解,对MVC模型工作原理和工作过程有了更深刻了解,对struts2模型及其工作过程也有了比以前更深认识,对于怎样配置web.xml和struts.xml文件也加深了记忆,Filterdispatcher过滤器作用自己也深入加深了印象。最关键是自己在这次课程设计中培养了自己独立觉问题能力和处理问题耐心。比如自己在计算收入和支出帐务和时候,刚开始自己所写算法之不能正确计算
40、出结果,总是在返回物理视图显示0.0 0.0,在运行期间控制没有显示任何错误,原来自己在初始化时候给incometotal和costtotal全部初始化为0.0而自己方法在实施查询语句后得到一个Resultset结果集自己在累加计算收入和支出综合时候,自己在if语句调减中rs.next()取非,造成没有进行累加计算,结果如论怎样全部返回0.0和0.0这两个结果。只要把去非!去掉即可得到正确计算结果并返回给物理视图。 参考文件 [1] 范立峰,林果园.JavaWeb程序设计教程.第1版, 北京:人民邮电出版社, 成绩评定
41、 成绩 老师签字 目录 1 设计时间 1 2 设计目标 1 3设计任务 1 4 设计内容 1 4.1 设计题目 1 4.1.1系统功效要求 1 4.1.2 数据库存放要求 1 4.1.3数据库设计 1 4.1.4系统结构关系 2 4.2 Jsp 页面设计 3 4.2.1登录界面 3 4.2.2登录成功界面 4 4.2.3查询结果界面 5 4.3.1Servlet类 6 4.3.2Dao类 10 4.3.3数据库连接类 14 5 总结和展望 15 参考文件 15 成绩评定 15






