《javaEE课程设计个人财务管理系统.docx》由会员分享,可在线阅读,更多相关《javaEE课程设计个人财务管理系统.docx(19页珍藏版)》请在冰豆网上搜索。
Ld.
rxiSofJ.
用户名称:
value=""xbrx/center>
v/center>
v/form>
4.2.2登录成功界面
欧in登录iit
图一5(登录成功查询界面)
代码如下:
<%
Stringusername=(String)session.getAttribute(”name");
if(username!
=null){%>
欢迎登录:<%=username%>
<%}%>
method="post">
arg=2"
增加帐目明细请输入:
输入日期:
v/center>
v/form>
vformaction="servlet/AccountController?
arg=1"method="post">
查询帐目明细请点击:
删除帐目信息请如下:
vformaction="servlet/AccountController?
arg=3"method="post">
请输入要删除帐目的日期:
method="post">
vformaction="servlet/AccountController?
arg=4"
起女台Id号:
终止Id号:
4.2.3查询结果界面
帐目序吕
也人热孤
附加洛注
Id期
1
1DO.O
n.o
ZQO1-1-1
S
U.Q
SCI.o
cost
ZCOl-1-5
3
ISC.O
u.a
Qwer
zanl-E-5
4
丄c
n.n
Qweir
2001-2-3
S
l&a.o
n.a
2Cin丄一4-1
6
ISO・0
2001-4-1
[7
123・0
asJF
2001-4-2
mo
123・0
2001^4-2
阪
0・0
123・G
asf
2001^4-2
10
4咲0
m0
hliKli
2001-5=1
11
4S.0
60
DP疋h
2001-E-l
[12-
20.0
0.0
J11J
2001-5-2
图一6(查询结果界面)
4.3Java方法设计
4.3.1Servlet类
控制增删改操作的servlet类代码设计如下:
HttpServletResponse
HttpServletResponse
publicclassAccountControllerextendsHttpServlet{
protectedvoiddoGet(HttpServletRequestrequest,response)
throwsServletException,IOException{doPost(request,response);
}
protectedvoiddoPost(HttpServletRequestrequest,response)
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.parselnt(request.getParameter("id"));
b=Integer.parselnt(request.getParameter("id"));
AccountDaoaccountDao=newAccountDao();
Listlist2=accountDao.caculateAccount(a,b);request.setAttribute("result",list2);
RequestDispatcher
rdt仁request.getRequestDispatcher("/Result1.jsp");
rdt1.forward(request,response);
}
publicvoidfindAllAccount(HttpServletRequest
HttpServletResponseresponse)
throwsServletException,IOException
{
AccountDaoaccountDao=newAccountDao();
Listlist=accountDao.findAIIAccount();
request.setAttribute("accounts",list);
RequestDispatcherrdt=request.getRequestDispatcher("/Result.jsp");
rdt.forward(request,response);
}
publicvoidsaveAllAccount(HttpServletRequest
HttpServletResponseresponse)
throwsServletException,IOException
{
request.setCharacterEncoding("gbk");
doubleincomemoney=0.0;
request,
request,
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.saveAIIAccount(myAccount);
}
catch(Exceptione){
e.printStackTrace();
}
this.findAIIAccount(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.findAIIAccoun"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.setld(rs.getlnt
(1));
account.setIncomemoney(rs.getDouble
(2));
account.setCostmoney(rs.getDouble(3));
account.setRecord(rs.getString(4));
account.setTime(rs.getString(5));list.add(account);
}
conn.commit();
returnlist;
}
catch(Exceptione){
e.printStackTrace();
}
finally
{
if(conn!
=null)
{
try{
conn.close();
}
catch(SQLExceptione){
e.printStackTrace();
}
returnlist;
publicbooleansaveAIIAccount(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();
conn.commit();
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();
conn.commit();
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);
}
conn.commit();
account.setIncomemoney(incometotal);
account.setCostmoney(costtotal);
Iist2.add(account);
returnlist2;
}
catch(SQLExceptione){
e.printStackTrace();
}
returnlist2;}
}
4.3.3数据库连接类
数据库连接代码如下:
publicclassDBCon{
publicstaticConnectiongetConnection()
{
String
url="jdbc:
microsoft:
sqlserver:
//localhost:
1433;databaseName=myaccountbase";
Stringuser="sa";
Stringpsw="sa";
Connectionconn=nuII;
try{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
catch(ClassNotFoundExceptione){
e.print