• 软件工程概论课堂测试一


    设计思想:创建一个User类,具有三个String类型变量,用来存放需要的属性值。创建一个UserDaoimpl类,用来写向数据库添加数据的函数,添加数据时只需调用此函数即可。创建一个UserException类,用来抛出异常信息。ValidateUtil用来对输入时未输入的数据进行提示。addInput用来显示首界面,add界面用来接收客户端传递过来的参数,并对参数值进行判断,符合条件调用添加函数将数据添加到数据库,不符合条件报出添加错误信息。

    源代码:

    User

    package com.jaovo.msg.model;
    
    public class User {
        private String coursename;
        private String teacher;
        private String address;
        public String getCoursename() {
            return coursename;
        }
        public void setCoursename(String coursename) {
            this.coursename = coursename;
        }
        public String getTeacher() {
            return teacher;
        }
        public void setTeacher(String teacher) {
            this.teacher = teacher;
        }
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
        
    
    }

    IUserDao

    package com.jaovo.msg.dao;
    
    import com.jaovo.msg.model.User;
    
    public interface IUserDao {
        public void add(User user);
    
    }

    UserDaoimpl

    package com.jaovo.msg.dao;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import com.jaovo.msg.Util.DBUtil;
    import com.jaovo.msg.Util.UserException;
    import com.jaovo.msg.model.User;
    
    public class UserDaoimpl implements IUserDao{
        @Override
        public void add(User user) {
            //获得链接对象
            Connection connection = DBUtil.getConnection();
            //准备sql语句
            String sql = "select count(*) from t_user2 where coursename = ?";
            //创建语句传输对象
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            try {
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, user.getCoursename());
                //接收结果集
                resultSet = preparedStatement.executeQuery();
                //遍历结果集
                /*while(resultSet.next()) {
                    if (resultSet.getInt(1) > 0) {
                        throw new UserException("用户已存在") ;
                    }
                }*/
                
                sql = "insert into t_user2(coursename,teacher,address) value (?,?,?)";
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, user.getCoursename());
                preparedStatement.setString(2, user.getTeacher());
                preparedStatement.setString(3, user.getAddress());
                preparedStatement.executeUpdate();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                //关闭资源
                DBUtil.close(resultSet);
                DBUtil.close(preparedStatement);
                DBUtil.close(connection);
            }
            
        }
    
    }

    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() {
            try {
                //1 加载驱动
                Class.forName("com.mysql.jdbc.Driver").newInstance();
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String user = "root";
            String password = "root";
            String url = "jdbc:mysql://localhost:3306/jaovo_msg";
            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
        }
        
    }

    ValidateUtil

    package com.jaovo.msg.Util;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    
    public class ValidateUtil {
        public static  boolean validateNull(HttpServletRequest request,String[] fileds) {
            boolean validate = true;
            //map对象用来装载不同的错误信息
            Map<String,String> errorMsg = new  HashMap();
            for(String filed :fileds) {
                String value = request.getParameter(filed);
                if (value == null || "".equals(value.trim())) {
                    validate = false;
                    errorMsg.put(filed, filed+"不能为空");
                }
                if (!validate) {
                    request.setAttribute("errormsg", errorMsg);
                }
                
                
            }
            
            return validate;
        }
        public static String showError(HttpServletRequest request , String filed) {
            Map<String, String> errorMsg = (Map<String,String>)request.getAttribute("errormsg");
            if (errorMsg == null) {
                return "";
            }
            String msg = errorMsg.get(filed);
            if (msg == null) {
                return "";
            }
            return msg;
        }
        
    }

    add.jsp

    <%@page import="com.jaovo.msg.Util.ValidateUtil"%>
    <%@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  coursename= request.getParameter("coursename");
        String  teacher= request.getParameter("teacher");
        String  address= request.getParameter("address");
        boolean validate = ValidateUtil.validateNull(request, new String[]{"coursename","teacher","address"});
        if(!validate){
    %>
        <jsp:forward page="addInput.jsp"></jsp:forward>
    <%
        }
        if(teacher.equals("王建民")||teacher.equals("刘立嘉")||teacher.equals("刘丹")||teacher.equals("王辉")||teacher.equals("杨子光"))
        {
            if(address.startsWith("一教")||address.startsWith("二教")||address.startsWith("基教")||address.startsWith("三教"))
            {
                User user = new User();
                user.setCoursename(coursename);
                user.setTeacher(teacher);
                user.setAddress(address);
                UserDaoimpl userDao = new UserDaoimpl();
                try{
                
                userDao.add(user);
                out.println("添加成功");
    
                }catch(UserException e){
        %>
            <h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2>
        <%
                }
            }
            else
            {
                out.println("添加失败,没有该教学楼");
            }
        }
        
        else
        {
            out.println("添加失败,没有该教师");
        }
        %>
    </html>

    addInput.jsp

     <%@page import="com.jaovo.msg.Util.ValidateUtil"%>
    <%@page import="java.util.Map"%>
    <%@ 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>
        <title>增加课程信息页面</title>
    </head>
    <body>
    <%--     <%Map<String,String> errorMsg = (Map<String,String>)request.getAttribute("errormsg"); --%>
    <%--     %> --%>
        <form action="add.jsp" method="get">
            <table align="center" >
                <tr>
                    <td>课程名称 : </td>
                    <td>
                        <input type="text" name="coursename" />
                        <%=ValidateUtil.showError(request, "coursename")%>
                    </td>
                </tr>
                    <tr>
                    <td>任课教师:</td>
                    <td>
                        <input type="text" name="teacher" />
                        <%=ValidateUtil.showError(request, "teacher")%>
                    </td>
                </tr>
                <tr>
                    <td>上课地点:</td>
                    <td>
                        <input type="text" name="address" />
                    <%=ValidateUtil.showError(request, "address")%>
                    </td>
                </tr>
                <tr align="center">
                    <td >
                        <input type="submit" value="保存" />
                    </td>
                </tr>
            </table>
        </form>
    </body>
    </html>

    运行成果截图:

    缺陷记录日志

    学生:孟庆淋

    日期:2017.11.28

    教员:王建民

    程序号:1

    日期 编号 类型 引入阶段 排除阶段 修复时间 修复缺陷
    2017.11.28 1 20 编码 编译 1min 书写问题
    描述:在进行判断时候将teacher变量写成了coursename变量

    时间记录日志

    学生:孟庆淋                                    教师:王建民

    日期:2017.11.28

    日期 开始时间 结束时间 中断时间 净时间 活动 备注
    11.28

    800

    8:50   50 听课 软件工程概论
      9:00 9:45   45 编写程序 课堂测试一
      17:00 17:35 5 30 编写博客园  

     

    项目计划总结

    学生:孟庆淋

    日期:2017.11.28

    教员:王建民

    程序号:1

    PSP2.1 Personal Software Process Stages Time(%)(/minute)
    Planning 计划 2
    Estimate 估计这个任务需呀多少时间 50
    Analysis 需求分析 2
    Coding Standrd 代码规范 4
    Design 具体设计 5
    Coding 具体编码 40
    Test 测试 1
    Resporting 报告 50

     

  • 相关阅读:
    你最喜欢的程序员漫画
    编程名言名句
    查看一个数是不是2的n次方
    程序员需要有多懒 ?- cocos2d-x 数学函数、常用宏粗整理
    C++中string、int、char之间转换
    cocos2d-x 中使用 srand((unsigned)time(NULL))重新设置一个随机种子
    ASP.NET MVC SignalR 配合VUE
    VS Visual Studio光标无法自动到行尾,点哪里就在哪里解决办法
    .NET Core Entity Framework 代码优先配置
    Image Bitmap转MemoryStream后,上传遇到问题,报Index异常
  • 原文地址:https://www.cnblogs.com/mqlblog/p/7911291.html
Copyright © 2020-2023  润新知