• 软件工程课堂小测01


    设计思想

      1、创建class

      2、创建数据库链接类,加载驱动和创建连接对象

      3、创建class添加类,用于向数据库中添加信息

      3、创建jsp登录界面

      4、创建后台jsp页面从登录界面接收用户输入的信息

      5、在后台jsp页面里判断接收到的信息与要求的内容是否相符

      6调用方法添加到数据库中

    源代码

    //User.java
    package com.jaovo.msg.User;
    
    public class User {
        private int id;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        private String classname;
        public String getClassname() {
            return classname;
        }
        public void setClassname(String classname) {
            this.classname = classname;
        }
        public String getClassteacher() {
            return classteacher;
        }
        public void setClassteacher(String classteacher) {
            this.classteacher = classteacher;
        }
        public String getClassadress() {
            return classadress;
        }
        public void setClassadress(String classadress) {
            this.classadress = classadress;
        }
        private String classteacher;
        private String classadress;
        
    }
    //DBUtil.java
    package com.jaovo.msg.Util;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    public class DBUtil {
        public static Connection getConnection() {
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            String name="root";
            String password="root";
            String url="jdbc:mysql://localhost:3306/jaovo_class";
            Connection connection = null ;
            try {
                connection = DriverManager.getConnection(url,name,password);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return connection;
        }
        
        public static void close(Connection connection) {
            if(connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
        public static void close(PreparedStatement preparedStatement) {
            if(preparedStatement != null) {
                try {
                    preparedStatement.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
        public static void close(ResultSet resultSet) {
            if(resultSet != null) {
                try {
                    resultSet.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    //UserDao.java
    package com.jaovo.msg.User;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    
    import com.jaovo.msg.Util.DBUtil;
    
    public class UserDao {
        public void add(User user) {
            Connection connection = DBUtil.getConnection();
            String sql = "insert into t_user(classname,classteacher,classadress) value(?,?,?)";
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            
            try {
                preparedStatement = connection.prepareStatement(sql);
                preparedStatement.setString(1, user.getClassname());
                preparedStatement.setString(2, user.getClassteacher());
                preparedStatement.setString(3, user.getClassadress());
                preparedStatement.executeUpdate();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                DBUtil.close(preparedStatement);
                DBUtil.close(connection);
            }
        }
    }
    <!-- ClassInput.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>Insert title here</title>
    </head>
    <body>
    <center>
        <form action="ClassAdd.jsp" >
            <table>
                <tr>
                    <td>
                        课堂名称:
                    </td>
                    <td>
                        <input type="text" name="classname"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        任课教师:
                    </td>
                    <td>
                        <input type="text" name="classteacher"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        课程地点:
                    </td>
                    <td>
                        <input type="text" name="classadress"/>
                    </td>
                </tr>
                <tr>
                    <td></td><td><input type="submit" value="提交" /></td>
                </tr>
            </table>
        </form>
    </center>
    </body>
    </html>
    <!-- ClassAdd.jsp -->
    <%@page import="com.jaovo.msg.User.UserDao"%>
    <%@page import="com.jaovo.msg.User.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>
        <%
            String classname = request.getParameter("classname");
            String classteacher = request.getParameter("classteacher");
            String classadress = request.getParameter("classadress");
            
            if(classadress.startsWith("一教")||classadress.startsWith("二教")||classadress.startsWith("三教")||classadress.startsWith("基教")){
                if(classteacher.equals("王建民")||classteacher.equals("刘立嘉")||classteacher.equals("刘丹")||classteacher.equals("王辉")||classteacher.equals("杨子光")){
                    
                    User user = new User();
                    user.setClassname(classname);
                    user.setClassteacher(classteacher);
                    user.setClassadress(classadress);
                    UserDao userDao = new UserDao();
                    try{
                    userDao.add(user);
                    }catch(NullPointerException e){
                        e.printStackTrace();
                    }
                    }
                 else{
                     %>
                         <jsp:forward page="teacherfailure.jsp"></jsp:forward>
                     <%
                 }
                
            }
            
            else{
        %>
                    <jsp:forward page="failure.jsp"></jsp:forward>
        <%
            }
            
            
        %>
    </html>
    <!-- failure.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>Insert title here</title>
    </head>
    <body>
        <h2>请输入正确的地址</h2>
    </body>
    </html>
    <%@ 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>Insert title here</title>
    </head>
    <body>
        <h2>请输入正确的教师</h2>
    </body>
    </html>

    运行截图

      登录界面

    输入的信息不符合要求,会跳转到登录失败的界面,并且也没有传入到数据库中

    输入正确的信息,数据库中会添加

    时间记录日志

    学生:王行行

    日期:2017/11/28

    课程:java web

    开始时间

    结束时间

    中断时间

    活动

    备注

    600

     8:00

    编程

    编程课堂作业(登录界面)

     

    缺陷记录日志

    学生:王行行

    日期:2017/11/28

    日期

    编号

    类型

    引入阶段

    排除阶段

    修复阶段

    修复缺陷

    2017/11/28

    1

    空指针异常

    编码

    编译

    5min

    描述:在调用java中的方法时(UserDao.adduser))需要抛出空指针异常的问题,即加入try{ }catch(空指针异常变量){

    }finally{ }

    日期

    编号

    类型

    引入阶段

    排除阶段

    修复阶段

    修复缺陷

    2017/11/28

    2

    数据库语句传输对象

    编码

    编译

    15min

    描述:在向数据库添加信息时需要调用到preparedStstement.executeUpdate();函数

  • 相关阅读:
    浅谈 DML、DDL、DCL的区别
    SQL优化
    Appium定位方式总结
    移动端自动化测试-AppiumApi接口详解
    移动端自动化测试-WTF Appium?
    Selenium-Css Selector使用方法
    Selenium-Switch与SelectApi接口详解
    Selenium-ActionChainsApi接口详解
    Selenium-WebDriverApi接口详解
    Selenium之前世今生
  • 原文地址:https://www.cnblogs.com/0710whh/p/7911790.html
Copyright © 2020-2023  润新知