资源描述
1 设计时间
2011年12月12日——12月16日
2 设计目的
JavaEE课程设计是对所学JavaEE与中间件课程的小结,是提高学生对所学知识综合应用能力的一种方式,是集中实践性环节之一。要求同学们对课程中所学习到的知识综合运用,开发有一定规模的Java Web程序。
3设计任务
设计生活质量衡量系统
(1)衡量标准说明:消费有两种支出:吃饭支出和 其它支出,如果在一段时间内,吃饭支出占总支出的比例超过50%,生活质量为贫困;如果在20-50%之间,为温饱;如果低于20%,生活质量为小康。
(2)功能描述:用户以合法身份登录系统后,才能进行所有操作;用户可以添加、查看和删除支出情况(支出类型(有两种:吃饭支出和其它支出)、额度(人民币)、日期)。
(3)添加支出情况
(4)查看支出情况
(5)删除支出情况
(6)统计在某个时间段内,生活质量属于哪个层次
4 设计内容
4.1 设计题目
生活质量衡量系统。
4.1.1系统功能要求
用只有拥有合法身份才能登录系统,用以合法身份登录后才能进行添加、查看和删除支出情况(支出类型(有两种:吃饭支出和其它支出)、额度(人民币)、日期)。
4.1.2 数据库存储要求
数据的存储要求:吃饭支出其他支出,日期。
4.1.3数据库的设计
图1—1 (数据库存储表)
图1—2(数据库存储数据)
4.1.4系统构造关系
登陆窗口
密码
no
yes
操作界面
计算
删除
插入
查询
图1—3(jsp页面构造)
Jsp页面传递参数
调用servlet类
参数
计算方法servlet
删除方法servlet
查询方法servlet
插入方法servlet
删除方法dao类
查询方法dao类
插入方法dao类
计算方法dao类
图1—4(Java类功能调用)
4.2 Jsp 页面设计
4.2.1登录界面
图1—4(登录界面)
代码如下:
<body>
<form action="login" method="post">
<center>欢迎登陆生活质量衡量系统!</center>
<center>用户名称:<input type="text" name="username" value=""><br>
<center>用户密码:<input type="password" name="userpsw" value=""><br></center>
<center><input type="submit" name="submit" value="登录"></center>
<center><input type="reset" name="reset" value="重写"></center>
</form>
</body>
4.2.2登录成功界面
图1—5(登录成功查询界面)
<body>
<form action="servlet/ListAllThingServlet">
<center>登陆成功!点击获得详细的信息~~</center>
<center><input type="submit" name="submit" value="显示所有信息"/></center>
</form>
</body>
4.2.3查询结果界面
图1—6(查询结果界面)
4.3Java方法设计
4.3.1Servlet类
(1)密码验证的Servlet类的代码如下:
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = (String)request.getParameter("username");
String userpsw = (String)request.getParameter("userpsw");
System.out.println(username);
String forward = "";
if(username.equals("123") && userpsw.equals("123")){
forward = "/success.jsp";
}else{
forward = "/error.jsp";
}
(2)显示所有信息的servlet类代码设计如下:
public class ListAllThingServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ThingDAO thingDAO=new ThingDAO();
List list=thingDAO.getAllThing();
request.setAttribute("allthing", list);
request.getRequestDispatcher("/showAllThing.jsp").forward(request, response);
}
}
(3)控制增删改操作的servlet类代码设计如下:
插入支出情况代码:
public class SaveOutServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
double x=0.0;
double y=0.0;
x=Double.parseDouble(request.getParameter("eating"));
y=Double.parseDouble(request.getParameter("others"));
String times=(String)request.getParameter("times");
ThingDAO thingDAO=new ThingDAO();
try
{
thingDAO.saveUser(x,y,times);
}
catch(Exception e)
{
e.printStackTrace();
}
List list=thingDAO.getAllThing();
request.setAttribute("allthing", list);
request.getRequestDispatcher("/showAllThing.jsp").forward(request, response);
}
}
删除支出情况代码:
public class DeleteOutServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
double x=0.0;
x=Double.parseDouble(request.getParameter("eating"));
ThingDAO thingDAO=new ThingDAO();
try
{
thingDAO.deleteUser(x);
}
catch(Exception e)
{
e.printStackTrace();
}
List list=thingDAO.getAllThing();
request.setAttribute("allthing", list);
request.getRequestDispatcher("/showAllThing.jsp").forward(request, response);
}
}
衡量生活质量代码:
public class Caculate extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String time1=request.getParameter("time1");
String time2=request.getParameter("time2");
ThingDAO thingDAO=new ThingDAO();
try
{
String resulte=thingDAO.caculate(time1, time2);
request.setAttribute("resulte1", resulte);
request.getRequestDispatcher("/caculateresulte.jsp").forward(request,response);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
4.3.2Dao类
(1)显示所有支出情况的Dao类代码设计如下:
public class ThingDAO {
public List getAllThing(){
Connection con=DBConnection.getConnection();
List list=new ArrayList();
try{
Statement ps=con.createStatement();//Statement("select * from moneying");
ResultSet rs=ps.executeQuery("select * from moneying");
while(rs.next()){
Thing thing=new Thing();
thing.setEating(rs.getDouble(1));
thing.setOthers(rs.getDouble(2));
thing.setTimes(rs.getString(3));
list.add(thing);
}
return list;
}catch(SQLException e){
e.printStackTrace();
}
finally{
if(con!=null){
try{
con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
return list;
}
(2)添加,删除和统计生活质量的Dao类代码设计如下:
public boolean saveUser(double m, double n,String q) throws Exception{
Connection con = DBConnection.getConnection();
String listSQL="insert into moneying values (?,?,?)";
PreparedStatement pstmt=con.prepareStatement(listSQL);
try{
pstmt.setDouble(1, m);
pstmt.setDouble(2, n);
pstmt.setString(3, q);
pstmt.executeUpdate();
mit();
return true;
}catch (SQLException e){
con.rollback();
e.printStackTrace();
}finally{
con.close();
}
return false;
}
public boolean deleteUser(double x)throws Exception{
Connection con=DBConnection.getConnection();
PreparedStatement pstmt=con.prepareStatement("delete from moneying where eating=?");
try{
pstmt.setDouble(1, x);
pstmt.executeUpdate();
mit();
return true;
}catch(SQLException e){
con.rollback();
e.printStackTrace();
}finally{
con.close();
}
return false;
}
public String caculate(String x1,String x2)//计算式温饱还是小康生活水平
{
Connection con=DBConnection.getConnection();
int i=0;
int j=0;
String SQL1="select id from moneying where times=x1";
String SQL2="select id from moneying where times=x2";
try
{
PreparedStatement psmt1=con.prepareStatement(SQL1);
PreparedStatement psmt2=con.prepareStatement(SQL2);
ResultSet rs1=psmt1.executeQuery();
ResultSet rs2=psmt1.executeQuery();
if(rs1.next())
{
i=rs1.getInt(4);
}
if(rs2.next())
{
j=rs2.getInt(4);
}
}
catch(SQLException e)
{
e.printStackTrace();
}
double eatingtotal=0.0;
double total=0.0;
int k=0;
for(k=i;k<=j;k++)
{
String SQL3="seclect eating,others from moneying where id=k";
try{
PreparedStatement psmt3=con.prepareStatement(SQL3);
ResultSet rs3=psmt3.executeQuery();
eatingtotal=eatingtotal+rs3.getDouble(1);
total=total+rs3.getDouble(1)+rs3.getDouble(2);
}
catch(SQLException e)
{
e.printStackTrace();
}
}
if(eatingtotal/total<0.2)
{
return "xiaokang";
}
else if(eatingtotal/total>0.5)
{
return "pinkun";
}
else{
return"wenbao";
}
}
}
4.3.3数据库连接类
数据库连接代码如下:
public class DBConnection {
public static Connection getConnection(){
final String driverName="com.microsoft.jdbc.sqlserver.SQLServerDriver";
final String url="jdbc:microsoft:sqlserver://localhost:1433;databasename=mydb";
try{
Class.forName(driverName);
}catch(ClassNotFoundException e){
e.printStackTrace();
}try{
Connection con=DriverManager.getConnection(url,"sa","sa");
con.setAutoCommit(false);
return con;
}catch(SQLException e){
e.printStackTrace();
}return null;
}
}
5 总结与展望
通过本次课程设计自己JavaWeb编程加深了理解,对MVC模型的工作原理和工作过程有了更深刻的理解,对struts2模型及其工作过程也有了比从前更深的认识,对于如何配置web.xml和struts.xml文件也加深了记忆,Filterdispatcher过滤器的作用自己也进一步加深了印象。
这个系统主要是我自己开发的,但也得到了老师和同学的很大帮助。我正在做系统的过程中遇到了很多问题,有的是知识存储不足,有的是考虑不够周全,之所以能够顺利实现基本功功能,离不开老师和同学的大力相助。事实证明,只靠自己是不能顺利完成一套功能完整的系统的,必须充分利用团队的力量。
开发一套系统,最重要的是细心,并不是一定要做到面面俱到,但也要充分考虑到客户的需求和现实意义,不管什么系统,只用运用到实际应用中,才具有先现实意义。所以在准备工作中要正确分析社会需求了解现实应用,画出流程图,把大体框架做好,然后再逐一细化。我们不可能做到面面俱到,但一定要做到步步扎实,作为一个程序编程人员,要保持清醒的头脑,以现实为依据,让自己的每一行代码都能实现自己的意义。
通过这次课程设计,我收获的不仅仅是课程上的知识得到实际应用,还有编程的基本习惯和开发系统时应注意的流程。
参考文献
[1] 范立峰,林果园.JavaWeb程序设计教程.第1版, 北京:人民邮电出版社,2010
成绩评定
成绩 教师签字
18
展开阅读全文