zoukankan      html  css  js  c++  java
  • 软件工程概论第一次课堂测试

    设计思想:

    首先建立一个User类,用于存储教师姓名,课程,以及教室名称,并且写上赋值和取值方法。

    然后建立一个工具类用于连接数据库、

    建立一个操作类用于访问数据库,对数据库进行添加,遍历。

    最后建立一个Tianbiao.jspBaocun.jsp俩个jsp文件,Tianbiao.jsp用于输出表,用户输入信息,Baocun.jsp用于判断输入的信息是否符合要求或者是否已经存在。

    源代码:

    package com.jaovo.msg.Model;
    
    public class User {
        private String Username;
        private String Kecheng;
        private String Didian;
        public String getUsername() {
            return Username;
        }
        public void setUsername(String username) {
            Username = username;
        }
        public String getKecheng() {
            return Kecheng;
        }
        public void setKecheng(String kecheng) {
            Kecheng = kecheng;
        }
        public String getDidian() {
            return Didian;
        }
        public void setDidian(String didian) {
            Didian = didian;
        }
    
    }
    工具类:
    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 {
                        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").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 = "sa";
                     String password = "zlq521415";
                     String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=Users";// 数据源
                
                     Connection con =null;
                try {
                     con = DriverManager.getConnection(url, user, password);
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }// 连接数据库对象
          
               return con;
        }
        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();
            }
        }
    }
    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
        }
        
    
    }
    package com.jaovo.msg.Util;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    
    public class UtilEmpty {
        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;
        }
        
    }
    操作类:
    package com.jaovo.msg.Dao;
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.servlet.http.HttpServletRequest;
    
    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.Model.User;
    
    public class UserDao extends User {
    
        
        public void addMessage(User user) {
            //获得连接对象
            Connection connection = DBUtil.getConnection();
            //创建语句传输对象
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            try {
                //遍历结果集
                String sql="insert into teacher (Kecheng,Username,Didian) values (?,?,?)";
                preparedStatement=connection.prepareStatement(sql);
                preparedStatement.setString(1, user.getKecheng());
                preparedStatement.setString(2, user.getUsername());
                preparedStatement.setString(3, user.getDidian());
                preparedStatement.executeUpdate();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                //关闭资源
                DBUtil.close(resultSet);
                DBUtil.close(preparedStatement);
                DBUtil.close(connection);
                
            }
        }
        public boolean load(User user) {
            boolean jud=true;
            //获得连接对象
               Connection connection = DBUtil.getConnection();
                //准备sql语句
               String sql = "select * from teacher 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((user.getUsername().equals(resultSet.getString("Username")))&&(user.getDidian().equals(resultSet.getString("Didian")))&&(user.getKecheng().equals(resultSet.getString("Kecheng"))))
                    {jud=false;}
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally{
                DBUtil.close(resultSet);
                DBUtil.close(preparedStatement);
                DBUtil.close(connection);
            }
            return jud;
        }
        public static  boolean validate(User user) 
        {
            boolean validate = false;
            String jiaoshi[]={"王建民","刘丹","刘立嘉","王辉"};
            for(int i=0;i<4;i++)
            {
                if(user.getUsername().equals(jiaoshi[i]))
                    validate=true;
            }
            
            return validate;
        }
        public static  boolean validate1(User user) {
            boolean validate = false;
            String didian[]={"基教","一教","二教","三教"};
            for(int i=0;i<4;i++)
            {
                if(user.getDidian().startsWith(didian[i]))
                    validate=true;
            }
            
            return validate;
        }
    }
    <%@page import="com.jaovo.msg.Dao.UserDao"%>
    <%@page import="com.jaovo.msg.Util.UtilEmpty"%>
    <%@page import="com.jaovo.msg.Util.UserException"%>
    <%@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>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body background="C:Users张强PicturesSaved Pictures/1505615532655.jpg">
    <%
        String kecheng = request.getParameter("kecheng");
        String username = request.getParameter("username");
        String didian = request.getParameter("didian");
        User user=new User(); UserDao userDao = new UserDao();
        user.setDidian(didian);user.setKecheng(kecheng);user.setUsername(username);
        boolean validate =UtilEmpty.validateNull(request, new String[]{"kecheng","username","didian"});
        if(!validate){
        %>
        <jsp:forward page="Tianbiao.jsp"></jsp:forward>
    <%
        }
        if(!userDao.load(user)){
            request.setAttribute("error1","该信息已经存在");
            %>
            <jsp:forward page="Tianbiao.jsp"></jsp:forward>
        <%
            }
        if(!userDao.validate(user))
        {
            request.setAttribute("error", "教师姓名不符合,请重新输入");
    %>
    <jsp:forward page="Tianbiao.jsp"></jsp:forward>
    <%
        }
        if(!userDao.validate1(user))
        {
            request.setAttribute("error", "教室名称不符合,请重新输入");
            %>
        <jsp:forward page="Tianbiao.jsp"></jsp:forward>
            <%
        }
        try{
        userDao.addMessage(user);
    %>
    保存成功<br>
    <%
        }catch(UserException e){
    %>
        <h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2>
        <%
        }
        %>
          
    </body>
    </html>
    <%@page import="com.jaovo.msg.Util.UtilEmpty"%>
    <%@page import="com.jaovo.msg.Dao.UserDao"%>
    <%@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>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
     <form action="Baocun.jsp" method="get">
    <body background="C:Users张强PicturesSaved Pictures/1505615532655.jpg">
    <%=request.getAttribute("error") %>
    <%if(request.getAttribute("error1")!=null)
        {
        out.print("<script language='javaScript'>alert('该信息已经存在');</script>");
        }
        %>
        
           <table align="center" border="1" width="500">
        <tr>
                    <td>课程名称 : </td>
                    <td>
                        <input type="text" name="kecheng" />
                        <%=UtilEmpty.showError(request, "kecheng") %>
                    </td>
                </tr>
                    <tr>
                    <td>任课教师:</td>
                    <td>
                        <input type="text" name="username" />
                        <%=UtilEmpty.showError(request, "username") %>
                    </td>
                </tr>
                <tr>
                    <td>教室地点:</td>
                    <td>
                        <input type="text" name="didian" />
                        <%=UtilEmpty.showError(request, "didian") %>
                        </td>
                        </tr>
                            <tr align="center">
                    <td colspan="2">
                        <input type="submit" value="保存" />
                        </table>
    </body>
    </form>
    </html>

    结果截图:

     

    实验分析:

    在教室写的时候我开始打算修改培训老师的代码,增加俩个jsp文件,但是写好之后Tomcat连不上,后来找到原因是因为项目中的没上课的时候xml文件我修改过,改好之后,我新建了一个web项目,开始自己写代码,遇到的错误大多是因为粗心,比如还未给user对象赋值就调用该对象,导致结果一直不对,改好之后,能实现基本操作,完善的时候借鉴了同学的弹出窗口代码,当信息存在的时候弹出。

    耗费时间:

    上课一节课,下课后吃过午饭后从一点开始到俩点多的时候完善完毕。

  • 相关阅读:
    Android2.3.7源码结构分析
    【开源推荐】AllJoyn:打造全球物联网的通用开源框架
    Ubuntu12.04安装JDK6
    ubuntu12.04升级后找不到共享目录
    Windows Embedded Compact 7新特性
    Windows Embedded Compact 2013 安装体验
    Windows CE,你妈吗喊你在多核上玩玩
    April Fools Contest 2017 C
    April Fools Contest 2017 B
    April Fools Contest 2017 A
  • 原文地址:https://www.cnblogs.com/zhangliqiangvictory/p/7910838.html
Copyright © 2011-2022 走看看