软件工程概论——课程导入界面以及数据导入数据库

将课堂练习设计思想,源程序代码、运行结果截图的开发过程,并按照PSP0级的要求记录开发过程中的项目计划日志、时间记录日志、缺陷记录日志。 

1、课堂练习设计思想:

  ①第一步就是先在.jsp文件中绘制出界面,采用h2绘制标题“课程导入”,加上分割线使界面更加美观,正体部分则使用<form><table>表单来完成布局,条理分明。

  ②在数据库中建立表kecheng,三列数据classname,teacher,place分别是课程名称、任课教师和上课地点。

  ③在src中建立工具包存放GBUtil.java用来连接数据库,采用utf-8字符集,防止中文乱码。

  ④建立KechengDao.java来实现接口IKecheng.java,包含一个add函数,用来完成向数据库中添加数据的操作。

  ⑤建立kecheng.jsp作为kechengjiemian.jsp的后台程序,取得输入的参数并判断验证,不符合规矩的都返回原界面,符合的才能成功保存到数据库。

  ⑥成功添加后点击继续添加超链接,返回导入界面继续操作。

2、程序源代码:

Ikecheng.java:

package kechengjiemian.gb.dao;
import kechengjiemian.gb.model.Kecheng;
public interface Ikecheng 
{
    public void add(Kecheng kecheng);
}

KechengDao.java

package kechengjiemian.gb.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import kechengjiemian.gb.Util.GBUtil;

import kechengjiemian.gb.model.Kecheng;
public class KechengDao implements Ikecheng
{

    @Override
    public void add(Kecheng kecheng)
    {
        Connection connection=GBUtil.getConnection();
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        String sql="insert into kecheng (classname,teacher,place) value (?,?,?);";
        try 
        {
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setString(1, kecheng.getClassName());
            preparedStatement.setString(2, kecheng.getTeacher());
            preparedStatement.setString(3, kecheng.getPlace());
            preparedStatement.executeUpdate();
        } 
        catch (SQLException e) 
        {
            e.printStackTrace();
        }
        finally
        {
            GBUtil.close(connection);
            GBUtil.close(preparedStatement);
            GBUtil.close(resultSet);
        }
    }
}

Kecheng.java

package kechengjiemian.gb.model;
public class Kecheng
{
    private String classname;
    private String teacher;
    private String place;
    public String getClassName() 
    {
        return classname;
    }
    public void setClassName(String classname) 
    {
        this.classname = classname;
    }
    public String getTeacher() 
    {
        return teacher;
    }
    public void setTeacher(String teacher)
    {
        this.teacher = teacher;
    }
    public String getPlace()
    {
        return place;
    }
    public void setPlace(String place)
    {
        this.place = place;
    }

}

GBUtil.java

package kechengjiemian.gb.Util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class GBUtil
{
    public static Connection getConnection()
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            
        }
        catch(ClassNotFoundException e)
        {
            e.printStackTrace();
            
        }
        String url="jdbc:mysql://localhost:3306/denglu?useUnicode=true&characterEncoding=utf-8";
        Connection connection=null;
        try 
        {
            connection=DriverManager.getConnection(url, "root", "242772");
            
        } 
        catch (SQLException e)
        {
            e.printStackTrace();
            System.out.println("数据库连接失败!");
        }
        return connection;    
    }
    public static void close(Connection connection)
    {
        try
        {
            if(connection!=null)
            {
                connection.close();
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
    }
    public static void close(PreparedStatement preparedStatement)
    {
        try
        {
            if(preparedStatement!=null)
            {
                preparedStatement.close();
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
    }
    public static void close(ResultSet resultSet)
    {
        try
        {
            if(resultSet!=null)
            {
                resultSet.close();
            }
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }
    }

}

kecheng.jsp

<%@page import="kechengjiemian.gb.model.Kecheng"%>
<%@page import="kechengjiemian.gb.Util.GBUtil"%>
<%@page import="kechengjiemian.gb.dao.KechengDao"%>
<%@ 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>
<%
    String classname=request.getParameter("classname");
    String teacher=request.getParameter("teacher");
    String place=request.getParameter("place");
    if(classname==null||"".equals(classname.trim()))
    {
        request.setAttribute("error", "课程名称不能为空!");
%>
<jsp:forward page="kechengjiemian.jsp"></jsp:forward>
<%
    }
    if(teacher==null||"".equals(teacher.trim()))
    {
        request.setAttribute("error", "任课教师不能为空!");
%>
<jsp:forward page="kechengjiemian.jsp"></jsp:forward>
<%
    }
    if(place==null||"".equals(place.trim()))
    {
        request.setAttribute("error", "上课地点不能为空!");
%>
<jsp:forward page="kechengjiemian.jsp"></jsp:forward>
<%
    }
    if(!teacher.trim().equals("王建民")&&!teacher.trim().equals("刘丹")&&!teacher.trim().equals("刘立嘉")&&!teacher.trim().equals("王辉")&&!teacher.trim().equals("杨子光"))
    {
        request.setAttribute("error", "这个老师不存在,请检查是否名字拼写错误!");
%>
<jsp:forward page="kechengjiemian.jsp"></jsp:forward>
<%
    }
    if(!place.trim().substring(0,2).equals("基教")&&!place.trim().substring(0,2).equals("一教")&&!place.trim().substring(0,2).equals("二教")&&!place.trim().substring(0,2).equals("三教"))
    {
        request.setAttribute("error", "您输入的地点不为教学楼");
%>
<jsp:forward page="kechengjiemian.jsp"></jsp:forward>
<%
    }
    Kecheng ke=new Kecheng();
    ke.setClassName(classname);
    ke.setTeacher(teacher);
    ke.setPlace(place); 
    KechengDao keDao=new KechengDao();
    keDao.add(ke);
    %>
    <h2 style="color:blue" align="center"><img src="../picture/正确.png">课程信息保存成功</h2>
    <form>
    <table  align="center" border="2" width="100">
    <tr align="center">
    <td colspan="2">
    <a href="kechengjiemian.jsp"><u>继续添加</u></a>
    </td>
    </tr>
    </table>
    </form>
    <%
%>
</html>

kechengjiemian.jsp

<%@ 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>
    <h2 align="center" style="color:blue">课程录入</h2>
    <hr size="5" align="center" style="color:blue">
    <form action="kecheng.jsp" method="get">
        <table align="center" border="2" width="300">
            <tr>
            <td>课程名称</td>
            <td>
                <input type="text" name="classname" />
            </td>
            </tr>
            <tr>
            <td>任课教师</td>
            <td>
                <input type="text" name="teacher" />
            </td>
            </tr>
            <tr>
            <td>上课地点</td>
            <td>
                <input type="text" name="place" />
            </td>
            </tr>
            <tr align="center">
            <td colspan="2">
                <input type="submit" name="提交" />
            </td>
            </tr>
        </table>
    </form>
        <br>
    <%
    if(request.getAttribute("error")!=null)
    {
    %>
    <h2 align="center" style="color:red"><img src="../picture/错误.gif"><%=request.getAttribute("error") %></h2>
    <%
    }
    %>
</body>
</html>

3、运行结果截图:

jsp调试的时候我设置的弹出到外置浏览器,这是初始界面:

  

 

录入信息:

  

在输入的信息都符合要求的时候,完成提交,并可以点击继续添加返回界面:

  

当老师的名字不符合要求:

  

当上课地点不符合要求:

  

当课程名称为空时:

  

当任课教师为空时:

  

当上课地点为空时:

  

4、项目计划日志:

  需求描述:完成一个能判断输入信息并将课程信息导入数据库的web程序。

  估计开发时间:四五十分钟。

  项目计划数据:填写完成。

  时间记录日志:填写完成。

  缺陷记录日志:填写完成。

  姓名:郭斌

  日期:2017/11/28

   

  今天共学习了6小时3分钟,其中完成课堂测试用了63分钟。

5、时间记录日志:

  学生:郭斌

  日期:2017/11/28

  教员:王建民

  课程:PSP

 

6、缺陷记录日志:

  学生:郭斌

  日期:2017/11/28

  教员:王建民

  程序号:1

   

7、个人总结: 

  今天上午课堂上写程序的时候,最后出现了乱码问题,之后修改字符集就解决了这个问题。这些错误可以不断的积累下来,从而更好的避开问题所在。发现自己写代码的速度过慢,在限时写代码的时候写不快,这是很伤的一点,还是要不断的练习,保证自己的书写规范以及编程速度。

posted @ 2017-11-28 18:57  我命倾尘  阅读(591)  评论(0编辑  收藏  举报