javaEE 课程设计 个人财务管理系统.docx
《javaEE 课程设计 个人财务管理系统.docx》由会员分享,可在线阅读,更多相关《javaEE 课程设计 个人财务管理系统.docx(18页珍藏版)》请在冰豆网上搜索。
javaEE课程设计个人财务管理系统
软件学院
课程设计报告书
课程名称JavaEE课程设计
设计题目个人财务系统
专业班级软件工程
学号xxxxxxxxx07
姓名xxxxx
指导教师姜彦吉
2011年12月
1设计时间
2011年12月12日——12月16日
2设计目的
JavaEE课程设计是对所学JavaEE与中间件课程的小结,是提高学生对所学知识综合应用能力的一种方式,是集中实践性环节之一。
要求同学们对课程中所学习到的知识综合运用,开发有一定规模的JavaWeb程序。
3设计任务
设计个人帐务管理系统,要求用户以合法的身份登录后可以对系统进行操作,用户可以查看,添加,删除和计算某段时间内帐务的收入和支出信息等。
帐务信息包括收入和支出两种,还有日期和备注。
4设计内容
4.1设计题目
个人财务管理系统
4.1.1系统功能要求
用只有拥有合法身份才能登录系统,用以合法身份登录后可以产看帐务信息、添加帐务信息、删除帐务信息、分别统计某个时间段内的收入和支出总额。
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系统构造关系
no
yes
图—2(jsp页面构造)
图—3(Java类功能调用)
4.2Jsp页面设计
4.2.1登录界面
图—4(登录界面)
代码如下:
欢迎登陆个人财务统计系统!用户名称:
用户密码:
4.2.2登录成功界面
图—5(登录成功查询界面)
代码如下:
<%
Stringusername=(String)session.getAttribute("name");
if(username!
=null){%>
欢迎登录:<%=username%>
<%}%>
arg=2"method="post">
增加帐目明细请输入:
收入数额:
支出数额:
附加备注:
输入日期:
arg=1"method="post">
查询帐目明细请点击:
删除帐目信息请如下:
arg=3"method="post">
请输入要删除帐目的日期:
arg=4"method="post">
起始Id号:
终止Id号:
4.2.3查询结果界面
图—6(查询结果界面)
4.3Java方法设计
4.3.1Servlet类
控制增删改操作的servlet类代码设计如下:
publicclassAccountControllerextendsHttpServlet{
protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
doPost(request,response);
}
protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException{
intarg=Integer.parseInt(request.getParameter("arg"));
switch(arg)
{
case1:
this.findAllAccount(request,response);
break;
case2:
this.saveAllAccount(request,response);
break;
case3:
this.deleteSomeAccount(request,response);
break;
case4:
this.caculateAccount(request,response);
break;
}
}
protectedvoidcaculateAccount(HttpServletRequestrequest,
HttpServletResponseresponse)
throwsServletException,IOException
{
inta=0;
intb=0;
a=Integer.parseInt(request.getParameter("id"));
b=Integer.parseInt(request.getParameter("id"));
AccountDaoaccountDao=newAccountDao();
Listlist2=accountDao.caculateAccount(a,b);
request.setAttribute("result",list2);
RequestDispatcherrdt1=request.getRequestDispatcher("/Result1.jsp");
rdt1.forward(request,response);
}
publicvoidfindAllAccount(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException
{
AccountDaoaccountDao=newAccountDao();
Listlist=accountDao.findAllAccount();
request.setAttribute("accounts",list);
RequestDispatcherrdt=request.getRequestDispatcher("/Result.jsp");
rdt.forward(request,response);
}
publicvoidsaveAllAccount(HttpServletRequestrequest,HttpServletResponseresponse)
throwsServletException,IOException
{
request.setCharacterEncoding("gbk");
doubleincomemoney=0.0;
doublecostmoney=0.0;
incomemoney=Double.parseDouble(request.getParameter("incomemoney"));
costmoney=Double.parseDouble(request.getParameter("costmoney"));
Stringrecord=(String)request.getParameter("record");
Stringtime=(String)request.getParameter("time");
MyAccountmyAccount=newMyAccount();
myAccount.setIncomemoney(incomemoney);
myAccount.setCostmoney(costmoney);
myAccount.setRecord(record);
myAccount.setTime(time);
AccountDaoaccountDao=newAccountDao();
try{
accountDao.saveAllAccount(myAccount);
}
catch(Exceptione){
e.printStackTrace();
}
this.findAllAccount(request,response);
}
publicvoiddeleteSomeAccount(HttpServletRequestrequest,
HttpServletResponseresponse)
throwsServletException,IOException
{
request.setCharacterEncoding("gbk");
Stringtime=request.getParameter("time");
MyAccountmyAccount=newMyAccount();
myAccount.setTime(time);
AccountDaoaccountDao=newAccountDao();
try{
accountDao.deleteSomeAccount(myAccount);
}
catch(Exceptione){
e.printStackTrace();
}
this.findAllAccount(request,response);
}
}
密码验证的Servlet类的代码如下:
publicclassLoginServletextendsHttpServlet{
publicvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)
throwsServletException,IOException{
doGet(req,resp);
}
publicvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)
throwsServletException,IOException{
Stringusername=req.getParameter("username");
Stringpassword=req.getParameter("password");
Useruser=newUser();
user.setUsername(username);
user.setPassword(password);
HttpSessionsession=req.getSession();
Stringforward="";
if(UserService.CheckLogin(user))
{
forward="/Search.jsp";
session.setAttribute("name",username);
}
else
{
forward="/error.jsp";
}
RequestDispatcherrd=req.getRequestDispatcher(forward);
rd.forward(req,resp);
}
}
4.3.2Dao类
密码验证的Dao类代码设计如下:
publicclassUserService{
publicstaticbooleanCheckLogin(Useruser)
{
if(user.getUsername().equals("lb")&&user.getPassword().equals("123"))
{
returntrue;
}
returnfalse;
}
}
帐务添加查询计算的Dao类代码设计如下:
publicclassAccountDao{
privateConnectionconn;
publicListfindAllAccount()
{conn=DBCon.getConnection();
StringlistSQL="select*frommyaccount";
Listlist=newArrayList();
try{
PreparedStatementpsmt=conn.prepareStatement(listSQL);
ResultSetrs=psmt.executeQuery();
while(rs.next())
{
MyAccountaccount=newMyAccount();
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();
returnlist;
}
catch(Exceptione){
e.printStackTrace();
}
finally
{
if(conn!
=null)
{
try{
conn.close();
}
catch(SQLExceptione){
e.printStackTrace();
}
}
}
returnlist;
}
publicbooleansaveAllAccount(MyAccountaccount)throwsException
{
conn=DBCon.getConnection();
StringlistSQL="insertintomyaccountvalues(?
?
?
?
)";
PreparedStatementpsmt=conn.prepareStatement(listSQL);
try{
psmt.setDouble(1,account.getIncomemoney());
psmt.setDouble(2,account.getCostmoney());
psmt.setString(3,account.getRecord());
psmt.setString(4,account.getTime());
psmt.executeUpdate();
mit();
returntrue;
}
catch(SQLExceptione){
conn.rollback();
e.printStackTrace();
}
returnfalse;
}
publicbooleandeleteSomeAccount(MyAccountaccount)throwsException
{
conn=DBCon.getConnection();
StringlistSQL="deletefrommyaccountwheretime=?
";
PreparedStatementpsmt=conn.prepareStatement(listSQL);
try{
psmt.setString(1,account.getTime());
psmt.executeUpdate();
mit();
returntrue;
}
catch(SQLExceptione){
conn.rollback();
e.printStackTrace();
}
returnfalse;
}
publicListcaculateAccount(intx,inty)
{
conn=DBCon.getConnection();
doubleincometotal=0.0;
doublecosttotal=0.0;
inti=0;
Listlist2=newArrayList();
try{
MyAccountaccount=newMyAccount();
for(i=x;i<=y;i++)
{
StringlistSQL1="selctincomemoneyfrommyaccountwhereid=i";
PreparedStatementpsmt1=conn.prepareStatement(listSQL1);
ResultSetrs1=psmt1.executeQuery();
psmt1.execute();
incometotal=incometotal+rs1.getDouble
(2);
StringlistSQL2="selectcostmoneyfrommyaccountwhereid=i";
PreparedStatementpsmt2=conn.prepareStatement(listSQL2);
ResultSetrs2=psmt2.executeQuery();
psmt2.execute();
costtotal=costtotal+rs2.getDouble(3);
}
mit();
account.setIncomemoney(incometotal);
account.setCostmoney(costtotal);
list2.add(account);
returnlist2;
}
catch(SQLExceptione){
e.printStackTrace();
}
returnlist2;}
}
4.3.3数据库连接类
数据库连接代码如下:
publicclassDBCon{
publicstaticConnectiongetConnection()
{
Stringurl="jdbc:
microsoft:
sqlserver:
//localhost:
1433;databaseName=myaccountbase";
Stringuser="sa";
Stringpsw="sa";
Connectionconn=null;
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
}
catch(ClassNotFoundExceptione){
e.printStackTrace();
}
try{
conn=DriverManager.getConnection(url,user,psw);
conn.setAutoCommit(false);
returnconn;
}
catch(SQLExceptione){
e.printStackTrace();
}
returnnull;}
}
5总结与展望
通过本次课程设计自己JavaWeb编程加深了理解,对MVC模型的工作原理和工作过程有了更深刻的理解,对struts2模型及其工作过程也有了比从前更深的认识,对于如何配置web.xml和struts.xml文