软件工程概论课堂测试02

1.程序设计思想

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

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

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

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

2.源程序代码

实现连接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 {

//信1605-2  20163691    陈美琪    
    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{
    //信1605-2  20163691    陈美琪
    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>

3.运行结果截图

 

4.项目计划日程

姓名:陈美琪                                                                                             日期:28/11/2017

             任务

日期

 

 

上课

 

 

课下编写程序

日总计

 

周二

 

100

180

280

 5.时间记录日志

学生:陈美琪                                                                                                     日期:28/11/2017

教师:王建民                                                                                                     课程;软件工程概论

时间

开始时间

结束时间

中断时间

净时间

活动

备注

11/28

8:00

9:50

10

100

上课

10:10

11:55

105

上课

11:55

12:30

35

吃饭

12:40

14:50

130

编程序

14:50

16:00

70

开会

16:20

18:00

100

编程序

6.缺陷记录日志

学生:陈美琪

日期:2017/11/28

教师:王建民

程序号:01

日期

编号

错误

引入阶段

排除阶段

修复时间

11/28

1

运行时总会报错HTTP Status 404

设计

运行

100

解决方法:加载了另外一个Tomcat。

2

使用sql server数据库连接不上

编译

运行

30

解决方法:使用MySQL数据库,开始使用sql server的用户名有错误,可能因为有些原因改变了数据库名称,可改为本机的IP地址。

原文地址:https://www.cnblogs.com/qilin20/p/7911351.html