软件工程概论--课后作业1

         作业概况:

                           

   1.网站系统开发所需技术

    1.基础内容
      网页设计概述、网站设计制作的基本流程、色彩搭配在网站中的应用、网站用户界面的设计、网站广告的设计、网站中表格的使用、网站中层的应用、框架网站的制作、模板网站的制作、使用行为和Javascript制作特效、使用css样式表设计网页、建设数据库网站、
       2、技术内容
    HTML语法、css语法、JavaScript语法
     3、图像处理
    Flash动画创意、GIF动画制作、网页图片处理
     4、行业网站实例
    个人网站、企业宣传网站、新闻资讯网站、教育网站、电子政务网站、旅游网站、免费资源网站、门户网站、电子商务网站
     5、后台编程
      *数据库:SQLServer设计、MySQL设计、Access设计
      *编程语言:ASP、JSP、VBScript、JavaScript、PHP、ASP.net
      *编程实例:文章发布系统、留言板、BBS、会员注册系统、在线购物网站
     6、网站管理
      网站维护、网站规划、网站管理、商业网站全程制作、商业网站开发规范

         

         2.源代码

    1.代码思想:

将用户信息设置一个User类,有编号、用户名、密码等私有属性,公有的get、set方法,然后 将用户信息操作设置一个接口IUserDao,利用UserDaoImpl类实现接口,实现具体操作。因为要实现数据库信息的查找,所以将连接数据库写入DBUtil类中,可能操作会出错,错误异常写在UserException类中,然后是应用页面显示,所以利用.jsp,设计一个主登录界面addInput.jsp,因为登录的实质即是在数据库中查找相应信息,若表中有此条信息,则显示表中信息、登录成功,若无,则登录失败查找界面:load.jsp

数据库的连接:1.加载驱动  2.创建连接对象 3.创建语句传输对象 4.接收结果集 5.遍历 6.关闭资源。

             2.源代码   

 User类:

 

package com.jaovo.msg.model;

public class User {
    private int id;
    private String username;
    
    private String password;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    

}

IUserDao类:

 

package com.jaovo.msg.dao;
import java.util.List;
import com.jaovo.msg.model.User;
public interface IUserDao {
    public void add(User user);
    public void delete(int id);
    public void update(User user);
    public User load(int id);
    public User load(String username);
    public List<User> load();
}

 

UserDaoImpl 类:

 

package com.jaovo.msg.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jaovo.msg.Util.DBUtil;
import com.jaovo.msg.Util.UserException;
import com.jaovo.msg.model.User;
import sun.net.www.content.text.plain;
public class UserDaoImpl implements IUserDao {

    @Override
    public void add(User user) {
        //获得连接对象
        Connection connection=DBUtil.getConnection();
        //准备sql语句
        String sql="select count(*)from t_user where username= ?";
        //创建语句传输对象
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        try {
             preparedStatement=connection.prepareStatement(sql);
             preparedStatement.setString(1, user.getUsername());
             //接收结果集
             resultSet=preparedStatement.executeQuery();
             //遍历结果集
             while(resultSet.next())
             {
                 if(resultSet.getInt(1)>0)
                     throw new UserException("用户已存在");
             }
             sql="insert into t_user(username,password) value (?,?)";
             preparedStatement=connection.prepareStatement(sql);
             preparedStatement.setString(1, user.getUsername());
             preparedStatement.setString(2, user.getPassword());
            
             preparedStatement.executeUpdate();
        } catch (SQLException e) {
            
            e.printStackTrace();
        }finally
        {
            //关闭资源
            DBUtil.close(resultSet);
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
            
        }
    }
    public void delete(int id) {
        Connection connection=DBUtil.getConnection();
        String sql="delete from t_user where id=?";
        PreparedStatement preparedStatement=null;
        try
        {
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            preparedStatement.executeUpdate();
        }catch(SQLException e)
        {
            e.printStackTrace();
        }finally
        {
            //关闭资源
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
            
        }
        
    }

    public void update(User user) {
        //获得连接对象
        Connection connection=DBUtil.getConnection();
        //准备sql语句
        String sql="update t_user set password=? where id= ?";
        //创建语句传输对象
        PreparedStatement preparedStatement=null;
        try {
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getPassword());
            preparedStatement.setInt(2, user.getId());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally
        {
            //关闭资源
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
            
        }
        
        
    }

    @Override
    public User load(int id) {
                //获得连接对象
                Connection connection=DBUtil.getConnection();
                //准备sql语句
                String sql="select * from t_user where id = ?";
                //创建语句传输对象
                PreparedStatement preparedStatement=null;
                ResultSet resultSet=null;
                //集合中只能放User对象
                User user=null;
                try {
                    preparedStatement=connection.prepareStatement(sql);
                    preparedStatement.setInt(1, id);
                    resultSet=preparedStatement.executeQuery();
                     //遍历结果集
                     while(resultSet.next())
                     {
                         user=new User();
                         user.setId(id);
                         user.setUsername(resultSet.getString("username"));
                         user.setPassword(resultSet.getString("password"));
                     }
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }finally
                {
                    //关闭资源
                    DBUtil.close(resultSet);
                    DBUtil.close(preparedStatement);
                    DBUtil.close(connection);
                    
                }
                
                 
        return user;
    }

    @Override
    public User load(String username) {
        //获得连接对象
        Connection connection=DBUtil.getConnection();
        //准备sql语句
        String sql="select * from t_user where username = ?";
        //创建语句传输对象
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        //集合中只能放User对象
        User user=null;
        try {
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1,username);
            resultSet=preparedStatement.executeQuery();
             //遍历结果集
             while(resultSet.next())
             {
                 user=new User();
                 user.setUsername(username);
                 user.setId(resultSet.getInt("id"));
                 user.setPassword(resultSet.getString("password"));
             } 
             
        } catch (SQLException e) {
            
            e.printStackTrace();
        }finally
        {
            //关闭资源
            DBUtil.close(resultSet);
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
            
        }  
          return user;    
    }

    @Override
    public List<User> load() {
        //获得连接对象
        Connection connection=DBUtil.getConnection();
        //准备sql语句
        String sql="select * from t_user ";
        //创建语句传输对象
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        List<User> users=new ArrayList<User>();
        User user=null;
        try {
            preparedStatement=connection.prepareStatement(sql);
             resultSet=preparedStatement.executeQuery();
             //遍历结果集
             while(resultSet.next())
             {
                 user=new User();
                 user.setId(resultSet.getInt("id"));
                 user.setUsername(resultSet.getString("username"));
                 user.setPassword(resultSet.getString("password"));
                 users.add(user);
             }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally
        {
            //关闭资源
            DBUtil.close(resultSet);
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
            
        }
        return users;
    }

}

 

DBUtil 类:

 

package com.jaovo.msg.Util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil {
    public  static  Connection getConnection() {
        
            //1 加载驱动
            try {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
            } catch (InstantiationException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (ClassNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        String user = "root";
        String password = "root";
        String url = "jdbc:mysql://localhost:3306/test";
        Connection connection = null;
        try {
            //2 创建链接对象connection
             connection = DriverManager.getConnection(url,user,password);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return connection;
    }
    
    //关闭资源的方法
    public static void close(Connection connection ) {
        try {
            if (connection != null) {
                connection.close();
            }
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void close(PreparedStatement preparedStatement ) {
        try {
            if (preparedStatement != null) {
                preparedStatement.close();
            }
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void close(ResultSet resultSet ) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
}

 

UserException 类:

 

package com.jaovo.msg.Util;

public class UserException extends RuntimeException{

    public UserException() {
        super();
        // TODO Auto-generated constructor stub
    }

    public UserException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
        // TODO Auto-generated constructor stub
    }

    public UserException(String message, Throwable cause) {
        super(message, cause);
        // TODO Auto-generated constructor stub
    }

    public UserException(String message) {
        super(message);
        // TODO Auto-generated constructor stub
    }

    public UserException(Throwable cause) {
        super(cause);
        // TODO Auto-generated constructor stub
    }
    

}

 

addInput.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
                <tr align="center">
                 <h2 style="color:black ; font-size:50px ;">用户登录页面</h2>
</head>
<body>
    <h2 style="color:red ;"><%=request.getAttribute("error") %></h2>
    <form action="load.jsp" method="get">
        <table align="center" border="1" width="500">
            <tr>
                <td>用户名称 : </td>
                <td>
                    <input type="text" name="username" />
                </td>
            </tr>
                <tr>
                <td>用户密码:</td>
                <td>
                    <input type="password" name="password" />
                </td>
            <tr align="center">
                <td colspan="2">
                    <input type="submit" value="登录" />
                    <input type="reset" value="重置" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

add.jsp

<%@page import="com.jaovo.msg.Util.UserException"%>
<%@page import="com.jaovo.msg.dao.UserDaoImpl"%>
<%@page import="com.jaovo.msg.model.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<%
    //接收客户端传递过来的参数
    String username = request.getParameter("username");
    String password = request.getParameter("password");
    if(username == null || "".equals(username.trim())){
        request.setAttribute("error", "用户名不能为空");
    
%>
    <jsp:forward page="addInput.jsp"></jsp:forward>
<%
    }
    User user2=new User();
    UserDaoImpl userDao = new UserDaoImpl();
     user2=userDao.load(username);  
         if((user2==null))
         {
          request.setAttribute("error", "用户不存在");
          %>
          <jsp:forward page="addInput.jsp"></jsp:forward>
          <% 
          }
          else if((!(user2.getPassword().contentEquals(password))&&user2!=null))
          {
          request.setAttribute("error", "用户密码输入错误");
          %>
          <jsp:forward page="addInput.jsp"></jsp:forward>
          <% 
          }
          else
          {
    //重定向response.sendRedirect("List.jsp");
    %>
    用户登录成功<br>
    <table align="center" border="1" width="500">
        <tr>
            <td>用户编号</td>
            <td>用户名称</td>
            <td>用户密码</td>
        </tr>
        <%
        %>
        <tr>
            <td> <%=user2.getId() %></td>
            <td> <%=user2.getUsername() %></td>
            <td> <%=user2.getPassword() %></td>
            
        </tr>
        <%
        %>
    </table>
    <a href="addInput.jsp">返回登录界面</a><br>    
<%
}
%>
</html>

       3.运行结果截图

表中信息:

    

 

先输入正确用户名和密码:3 3

 

显示界面:

 

 

输入正确用户名:1 错误密码2

显示:

 

 

输入用户名、密码错误:6 6

 

4.我希望自己会成为自愿在深水区游泳的人,会努力在深水区生存。

目标:希望自己可以在团队作业中培养团队精神,独立的开发一项简单软件,对于软件生命周期的各个阶段有实际的了解,对于软件设计有实际的掌握,对敏捷软件开发的具体技术有实践能力。

周二 :早晨-下午 完成作业和自行拓展(也可归纳)

周四 :中午-下午 课后复习和归纳

周天 :早晨   预习和自行拓展

 

 

 

 

转载于:https://www.cnblogs.com/zhao-teng-ass/p/7885250.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值