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

    1 设计思想

    首先定义一个类来存放这三个课程信息。然后通过连接数据库,将用户输入的数据添加进去。这是初步阶段。完成对数据库的添加之后。之后,要判断用户输入的内容是否符合要求。加入一些判断。

    2 源代码

    package com.kao.msg.dao;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import com.kao.msg.Util.DBUtil;
    import com.kao.msg.model.Kecheng;
    
    public class Xinxi implements Ixinxi {
        public void add(Kecheng kecheng){
            Connection connection = DBUtil.getConnection();
            String sql ;
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            sql = "insert into t_add(name,teacher,address) values (?,?,?)";
            try {
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, kecheng.getName());
                preparedStatement.setString(2,kecheng.getTeacher());
                preparedStatement.setString(3, kecheng.getAddress());
                preparedStatement.executeUpdate();
                
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }    finally {
                //关闭资源
                DBUtil.close(resultSet);
                DBUtil.close(preparedStatement);
                DBUtil.close(connection);
            }
            
            
            
            
        }
    
    }
    增加
    package com.kao.msg.model;
    
    public class Kecheng {
        String teacher;
        String name;    
        String address;
        public String getTeacher() {
            return teacher;
        }
        public void setTeacher(String teacher) {
            this.teacher = teacher;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
        
    
    }
    View Code
    package com.kao.msg.Util;
    
    import java.sql.*;
    
    public class DBUtil {
        public  static  Connection getConnection(){
            try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String user = "sa";
            String password = "20163433";
            String url = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=kaoshi";
            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();
            }
        }
        
    
    
    }
    数据库连接
    package com.kao.msg.Util;
    
    public class JisuanException extends RuntimeException{
    
        public JisuanException() {
            super();
            // TODO Auto-generated constructor stub
        }
    
        public JisuanException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
            super(message, cause, enableSuppression, writableStackTrace);
            // TODO Auto-generated constructor stub
        }
    
        public JisuanException(String message, Throwable cause) {
            super(message, cause);
            // TODO Auto-generated constructor stub
        }
    
        public JisuanException(String message) {
            super(message);
            // TODO Auto-generated constructor stub
        }
    
        public JisuanException(Throwable cause) {
            super(cause);
            // TODO Auto-generated constructor stub
        }
    
    }
    异常
    <%@ 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>
    <%if(request.getAttribute("error1")!=null){
        out.print("<script language='javaScript'>alert('课程不能为空');</script>");
    }
    if(request.getAttribute("error2")!=null){
        out.print("<script language='javaScript'>alert('教师不能为空');</script>");
    }
    if(request.getAttribute("error3")!=null){
        out.print("<script language='javaScript'>alert('教室不能为空');</script>");
    }
    if(request.getAttribute("error4")!=null){
        out.print("<script language='javaScript'>alert('教师格式错误');</script>");
    }
    if(request.getAttribute("error5")!=null){
        out.print("<script language='javaScript'>alert('教室格式错误');</script>");
    }
    %>
    <form action="add.jsp" method="get">
    <table align="center" border="1" width="500">
                <tr>
                    <td>课程名称 : </td>
                    <td>
                        <input type="text" name="name" />
                    </td>
                </tr>
                <tr>
                    <td>任课教师:</td>
                    <td>
                        <input type="text" name="teacher" />
                    </td>
                </tr>
                <tr>
                    <td>上课地点:</td>
                    <td>
                        <input type="text" name="address" />
                    </td>
                </tr>
    
                <tr align="center">
                    <td colspan="2">
                        <input type="submit" value="提交" /><br>
                    </td>
                </tr>
            </table>
    </body>
    </html>
    addinput
    <%@page import="com.kao.msg.model.Kecheng"%>
    <%@page import="com.kao.msg.dao.Xinxi"%>
    <%@ 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>
    <%
        //接收客户端传递过来的参数
        String name = request.getParameter("name");
        String teacher = request.getParameter("teacher");
        String address = request.getParameter("address");
        Kecheng kecheng=new Kecheng();
        if(name == null || "".equals(name.trim())){
            request.setAttribute("error1", "课程不能为空");
        
    
    %>
        <jsp:forward page="addinput.jsp"></jsp:forward>
    <%
        }
        if(teacher == null || "".equals(teacher.trim())){
            request.setAttribute("error2", "用户名不能为空");
        
    
    %>
        <jsp:forward page="addinput.jsp"></jsp:forward>
    <%
        }
        if(address == null || "".equals(address.trim())){
            request.setAttribute("error3", "用户名不能为空");
        
    
    %>
        <jsp:forward page="addinput.jsp"></jsp:forward>
    <%
        }
        if(teacher.equals("王建民")||teacher.equals("刘力嘉")||teacher.equals("刘丹")||teacher.equals("王辉")||teacher.equals("杨子光")){
    %>
            <h1>教师保存成功!!<h1><br>
    <%    
        }
        else{
            request.setAttribute("error4", "无此教师");
    %>
            <jsp:forward page="addinput.jsp"></jsp:forward>
    
    
    <%
        }
        
            if(address.startsWith("一教")||address.startsWith("二教")||address.startsWith("三教")||address.startsWith("基教")){
                kecheng.setName(name);
                kecheng.setTeacher(teacher);
                kecheng.setAddress(address);
                Xinxi xinxi=new Xinxi();
                xinxi.add(kecheng);
        
        
    %>
    <h1>教室保存成功!!<h1><br>
    
    
    <%
            }
            else{
                request.setAttribute("error5", "教室格式错误");
    %>
                <jsp:forward page="addinput.jsp"></jsp:forward>
    <% 
            }
    
    %>
    </body>
    </html>
    add

    3 运行结果截图

    项目计划总计

    日期

    任务

    听课

    编写程序

    日总结

    周二

    上午

    50

    50

    下午

    30

    130

    时间记录日志

    日期

    开始时间

    中断时间

    净时间

    活动

    备注

    11/28

    上午

    8:00

    8:50

    50

    听课

    Psp讲解

    9:00

    9:50

    50

    编程序

    课堂测试

    下午

    1:50

    2:15

    25

    编程序

    完善课堂测试内容

    缺陷记录日志

    日期

    编号

    类型

    引入阶段

    排除阶段

    修复时间

    修复缺陷

    11/28

    1

    3min

    描述:忘记导入数据库的jar包,导致报错。

    2

    2min

    描述:数据库存入的内容为空。原因是在赋值阶段应该使用set方法,误用get方法导致赋值失败,将空值存入了数据库

    3

    25min

    描述:用户输入界面不能输入空值,而且输入不符合要求的信息所提示的错误不明确。然后我在jsp中添加了一些if判断,使程序更加健壮。

  • 相关阅读:
    [NOIP2011提高组]聪明的质监员
    NOIP 2010 关押罪犯
    题目:埃及分数
    用scanf输入long long 型的数
    poj 1014 Dividing
    Cactus
    SQLite数据库的增删改查代码
    UltraGrid常用方法属性代码
    维护数据表常用SQL语句
    C#备份收藏夹代码
  • 原文地址:https://www.cnblogs.com/wys-373/p/7909758.html
Copyright © 2011-2022 走看看