超级课程表

1程序设计思想

首先在addInput.jsp 界面中完成网页界面的设计,进行网页测试,看是否完成设计,然后在数据库中建立表,在daoimpi 类文件中进行数据的链接,进行测试,在add.jsp文件中进行判断输入的字符是否合法,建立一个自定义异常类ClassException,在try 中进行输入字符的判断,然后进行抛出异常,最后catch异常,用标签将错误原因显示在网页上

2源程序代码

IUserDao.java

 

package com.jaovo.msg.dao;



import com.jaovo.msg.mobeI.User;

public interface IUserDao {
    public void add(User user);//添加

}

UserdaoImpI.java  数据库的链接

package com.jaovo.msg.dao;//实现方法的类

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Connection;
import com.jaovo.msg.Util.DBUtil;
import com.jaovo.msg.Util.UserException;
import com.jaovo.msg.mobeI.User;


public class UserdaoImpI implements IUserDao
{//实现接口中的所有方法   6步
    

    @SuppressWarnings("resource")
    @Override
    public void add(User user) 
    {//增加的方法
        // TODO Auto-generated method stub
        Connection connection=DBUtil.getConnection();//获得连接对象
        //准备sql语句
        String sql = "select count(*) from t_user where username = ?";//count 数据的条数
        //创建语句传输对象
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;//
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getKecheng());
            resultSet = preparedStatement.executeQuery();//接受一下结果集
            //遍历结果集
            while(resultSet.next()) {
                if(resultSet.getInt(1) > 0) {
                    throw new UserException("用户已存在") ;
                    };
                }
        sql = "insert into t_user(username,password,nickname) values (?,?,?)";
        preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, user.getKecheng());
        preparedStatement.setString(2, user.getTeacher());
        preparedStatement.setString(3, user.getAdress());
        preparedStatement.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(resultSet);
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
    }
}

    

User。Java

package com.jaovo.msg.mobeI;

public class User {
    private int id;
    private String kecheng;
    private String teacher;
    private String adress;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getKecheng() {
        return kecheng;
    }
    public void setKecheng(String kecheng) {
        this.kecheng = kecheng;
    }
    public String getAdress() {
        return adress;
    }
    public void setAdress(String adress) {
        this.adress = adress;
    }
    public String getTeacher() {
        return teacher;
    }
    public void setTeacher(String teacher) {
        this.teacher = teacher;
    }
    
}

ClassException.java

package com.jaovo.msg.Util;

public class ClassException extends Exception{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public ClassException() {
        super();
        // TODO Auto-generated constructor stub
    }

    public ClassException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
        // TODO Auto-generated constructor stub
    }

    public ClassException(String message, Throwable cause) {
        super(message, cause);
        // TODO Auto-generated constructor stub
    }

    public ClassException(String message) {
        super(message);
        // TODO Auto-generated constructor stub
    }

    public ClassException(Throwable cause) {
        super(cause);
        // TODO Auto-generated constructor stub
    }

    
    
}

DBUtil.java

package com.jaovo.msg.Util;//链接数据库的方法
/*
 1加载驱动
 2创建连接对象
 3创建语句传输对象
 4接受结果集对象
 5遍历
 6关闭资源
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.tomcat.dbcp.dbcp2.DriverManagerConnectionFactory;

public class DBUtil {
    public static Connection getConnection() {
        //1.加载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String user = "root";
        String password = "root";
        String url = "jdbc:mysql://localhost:3306/jaovo_msg?useUnicode=true&characterEncoding=UTF-8";//连接数据库的代码
        Connection connection = null;//提升定域
        try {
            //2创建连接对象
            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();
        }
    }
}

ValidateUtil.java

package com.jaovo.msg.Util;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
public class ValidateUtil 
{
    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;
    }
}

add.jsp

<%@page import="com.jaovo.msg.Util.ClassException"%>
<%@page import="com.jaovo.msg.Util.UserException"%>
<%@page import="com.jaovo.msg.dao.UserdaoImpI"%>
<%@page import="com.jaovo.msg.mobeI.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>

<% //接受客户端传递过来的参数
    request.setCharacterEncoding("UTF-8");
    String kecheng = request.getParameter("kecheng");
    String teacher = request.getParameter("teacher");
    String adress = request.getParameter("adress");
    try{
        if(!teacher.trim().equals("王建民")&&!teacher.trim().equals("刘立嘉")&&!teacher.trim().equals("刘丹")&&!teacher.trim().equals("杨子光")&&!teacher.trim().equals("王辉"))
        {
            throw new ClassException("不能输入");
        }
        else if (!adress.trim().startsWith("基教")&&!adress.trim().startsWith("一教")&&!adress.trim().startsWith("二教")&&!adress.trim().startsWith("三教"))
        {
            throw new ClassException("不能输入");
        }
        else 
        {
            User user = new User();
            user.setKecheng(kecheng);
            user.setTeacher(teacher);
            user.setAdress(adress);
            UserdaoImpI userDao = new UserdaoImpI();
            userDao.add(user);
            %>
               
<body>
    用户保存成功!!<br>
    <a href="addInput.jsp">继续添加</a><br>
    <a href="#">用户列表</a>
            <% 
        }
    }
    catch(ClassException e){
        %>
            <h2 style="color:red ; font-size:50px">发生错误 : <%=e.getMessage() %></h2>
        <%
            }

    %>


</html>

addInput.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>
    <title>课程添加界面</title>
</head>        
<body>
    <form action = "add.jsp" method = "post">
        <table align = "center" border="1" width="500" >
            <tr>
                <td>课程名称:</td>
                <td>
                    <input type ="text" name = "kecheng"/>
                </td>    
            </tr>
                <tr>
                    <td>任课教师:</td>
                <td>
                    <input type = "text" name = "teacher"/>
                </td>
                <tr>
                    <td>上课地点:</td>
                    <td>
                        <input type = "text" name = "adress"     />
                    </td>
                </tr>
                <tr align="center">
                    <td colspan = "2">
                        <input type = "submit" value = "保存"/>
                        
                        </td>
        </table>
        
    </form>
</body>
</html>

程序执行结果截图

 

psp日志

日期

开始时间

结束时间

中断时间

净时间

活动

备注

C

U

2017.11.28

13.00

15.00

2.30

110分钟

休息

 

 

 

 

15.00

15.40

 

40

开班会

进行学习十九大

 

 

 

 

16.00

17.30

 

90

编程

 

 

 

 

17.30

17.50

 

30

吃饭

 

 

 

 

18.00

18.40

 

 

休息,聊天

 

 

 

 

18.40

21.00

20.00

110

编程

准备考试

 

 

 

 

 

任务

听课

编程

准备考试

总计时间(分钟)

11.21

80

50

30

160

11.22

N

60

40

100

11.23

N

30

40

70

11.24

160

50

40

250

11.25

N

180

40

220

11.26

N

30

0

30

11.27

N

60

30

90

11.28

80

90

40

210

 

 

日期

编号

类型

引入阶段

修复阶段

缺陷描述

类型

11.28

1

20

编码

测试阶段

在进行传入的参数判断是否为空的时候,误写入一个死循环,导致程序运行出错

编写错误

 

2

20

编码

测试

异常运行出错,在进行抛出异常的时候,自定义的异常编写错误

编写出错

 

 

 

 

 

 

 

 

 

总结:在进行有关项目的编码是,需要注意编程上的规范,在写完每一个部分之后都需要进行测试,防止程序出现不可逆转的错误,加大程序完成的效率,将时间更多的用来学习只是和巩固住已经学习的知识。

posted @ 2017-11-28 18:15  tomhaha  阅读(577)  评论(0编辑  收藏  举报