zoukankan      html  css  js  c++  java
  • 增加信息

    1. 题目要求:

     

    按照图片要求设计添加新课程界面。(0.5分)

    在后台数据库中建立相应的表结构存储课程信息。(0.5分)

    实现新课程添加的功能。

    要求判断任课教师为王建民、刘立嘉、刘丹、王辉、杨子光五位教师的其中一位。(0.5分)

    要求上课地点开头为“一教、二教、三教、基教”中的一种。(0.5分)

    实现数据存储功能。(3分)

    1. 设计思想

    在类中定义连接MySQL的方法,实现添加课程信息的方法,以及关闭资源的方法。

    ⑵定义类,类中自己定义各种异常处理。

    html文件中,绘制界面,对于任课教师以及上课地点的限制利用下拉菜单控制。

    JSP文件中,先接受用户传递过来的参数,调用类中定义的添加课程信息的函数,成功,则 “显示添加课程成功!”。

    1. 源程序代码

    实现连接MySQL数据库

    package Util;

     

    import java.sql.Connection;

    import java.sql.DriverManager;

    import java.sql.PreparedStatement;

    import java.sql.ResultSet;

    import java.sql.SQLException;

     

    public class AddCourse {

     

      

        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 = "chen123";

            String url = "jdbc:mysql://localhost:3306/course";

            Connection connection = null;

            try {

                //2 创建链接对象connection

                 connection = DriverManager.getConnection(url,user,password);

            } catch (SQLException e) {

                // TODO Auto-generated catch block

                //e.printStackTrace();

                System.out.println("连接失败!");

            }

            return connection;

        }

        

        

        

        public void add(String name,String teacher,String location) {

            //获得链接对象

            Connection connection = getConnection();

            //准备sql语句

            String sql = "select count(*) from course_1 where courseName = ?";

            //创建语句传输对象

            PreparedStatement preparedStatement = null;

            ResultSet resultSet = null;

            try {

                preparedStatement = connection.prepareStatement(sql);

                preparedStatement.setString(1, name);

                //接收结果集

                resultSet = preparedStatement.executeQuery();

                //遍历结果集

                while(resultSet.next()) {

                    if (resultSet.getInt(1) > 0) {

                        throw new UserException("用户已存在") ;

                    }

                }

                

                sql = "insert into course_1(courseName,teacherName,location) value (?,?,?)";

                preparedStatement = connection.prepareStatement(sql);

                preparedStatement.setString(1, name);

                preparedStatement.setString(2, teacher);

                preparedStatement.setString(3, location);

                preparedStatement.executeUpdate();

            } catch (SQLException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }finally {

                //关闭资源

                close(resultSet);

                close(preparedStatement);

                close(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 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

        }

        

    }

    绘制界面

    <!DOCTYPE html>

    <html>

    <head>

    <meta charset="UTF-8">

    <title>添加课程信息</title>

    </head>

    <body>

    <center>

            <h1 style="color:black">添加课程信息</h1>

            <form action="addcourse.jsp" method="get">

                <table border="0">

                    <tr>

                        <td>课题名称</td>

                        <td>

                            <input type="text" maxlength="8" name="name">

                        </td>

                    </tr>

                    <tr>

                        <td>任课教师:</td>

                        <td>

                            <select name="teacher">  

                                <option value="王建民">王建民</option>  

                                <option value="刘立嘉">刘立嘉</option>  

                                <option value="杨子光">杨子光</option>

                                <option value="刘丹">刘丹</option>

                                <option value="王辉">王辉</option>

                            </select>

                        </td>

                    </tr>

                    <tr>

                        <td>上课地点:</td>

                        <td>

                            <select name="point">  

                                <option value="一教">一教</option>  

                                <option value="二教">二教</option>  

                                <option value="三教">三教</option>

                                <option value="基教">基教</option>  

                            </select>

                        </td>

                    </tr>

                </table>

            </form>

            <input type="button" value="保存" onclick="confir()">

        </center>

    </body>

    </html>

     

    <script language="javascript">

    function confir(){

        var n=document.forms[0].name.value;

        if(n==""){

            alert("课程名称输入为空!");

        }

        else{

            document.forms[0].submit();

     

        }

     

    }

    </script>

    在界面中进行输入并添加信息

    <%@page import="Util.AddCourse"%>

     

     

    <%@ 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>添加课程</title>

    </head>

    <body>

     

    <%

        request.setCharacterEncoding("UTF-8");

        //接收客户传递过来的参数

        String courseName = request.getParameter("courseName");

        String teacherName = request.getParameter("teacherName");

        String location = request.getParameter("location");

        AddCourse add=new AddCourse();

     

    try{

        add.add(courseName, teacherName, location);

        out.print("<script language='javaScript'> alert('添加课程成功!');</script>");

        response.setHeader("refresh", "0;url=course.html");

    }

    catch(Exception e){

        out.print("<script language='javaScript'> alert('"+e.getMessage()+"');</script>");

        response.setHeader("refresh", "0;url=course.html");

    }

    %>

    </body>

    </html>

    1. 运行结果截图

     

     

     

     

    PSP2.1

    Personal Software Process Stages

    Planning

    计划

      · Estimate

      · 估计这个任务需要多少时间

    10小时

    Development

    开发

      · Analysis

      · 需求分析 (包括学习新技术)

    20分钟

      · Design Spec

      · 生成设计文档

      · Design Review

      · 设计复审 (和同事审核设计文档)

    10分钟

      · Coding Standard

      · 代码规范 (为目前的开发制定合适的规范)

    10分钟

      · Design

      · 具体设计

    40分钟

      · Coding

      · 具体编码

    6小时

      · Code Review

      · 代码复审

    1小时

      · Test

      · 测试(自我测试,修改代码,提交修改)

    1小时

    Reporting

    报告

    20分钟

      · Test Report

      · 测试报告

    20分钟

      · Size Measurement

      · 计算工作量

    1小时

      · Postmortem & Process Improvement Plan

      · 事后总结, 并提出过程改进计划

    合计11小时

  • 相关阅读:
    单元测试-伪对象、桩对象、模拟对象
    单元测试那些事
    vscode 开发go版本1.14项目
    C#中的 async await
    一手遮天 Android
    一手遮天 Android
    一手遮天 Android
    一手遮天 Android
    一手遮天 Android
    一手遮天 Android
  • 原文地址:https://www.cnblogs.com/877612838zzx/p/8301857.html
Copyright © 2011-2022 走看看