软件工程概论课堂测试一

Posted peanut

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了软件工程概论课堂测试一相关的知识,希望对你有一定的参考价值。

一、设计思想

  以向数据库的表中添加数据作为题目中的各项数据,运用数据库的增添以及连接数据库。

 

二、源代码

add.jsp

<%@page import="com.jaovo.msg.Util.UserException"%>
<%@page import="com.jaovo.msg.dao.UserDaoImpl"%>
<%@page import="com.jaovo.msg.model.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 username = request.getParameter("username");
    String password = request.getParameter("password");
    String nickname = request.getParameter("nickname");
    if(username == null || "".equals(username.trim())){
        request.setAttribute("error", "");
    
%>
    <jsp:forward page="addInput.jsp"></jsp:forward>
<%
    }
    User user = new User();
    user.setUsername(username);
    user.setPassword(password);
    user.setNickname(nickname);
    
    UserDaoImpl userDao = new UserDaoImpl();
    

    if(!username.equals("王建民")&&!username.equals("刘立嘉")&&!username.equals("刘丹")&&!username.equals("王辉")&&!username.equals("杨子光"))
    {
        %>
        <script language=\'javascript\'>alert("没有此教师");window.history.back(-1);</script>
        <% 
    
    }
    else if(!password.startsWith("基教")&&!password.startsWith("一教")&&!password.startsWith("二教")&&!password.startsWith("三教"))
    {
        %>
        <script language=\'javaScript\'>alert(\'没有此教室\');window.history.back(-1);</script>
         <% 
    }
    else
    {
        userDao.add(user);
    


    
%>


    课程保存成功!!<br>
    <a href="addInput.jsp">继续添加</a><br>
    <a href="#">课程列表</a>
<%
    }
%>
    
    
</html>

addIput.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>
    <%=request.getAttribute("error") %>
    <form action="add.jsp" method="get">
        <table align="center" border="1" width="500">
            <tr>
                <td>课程名称: </td>
                <td>
                    <input type="text" name="nickname" />
                </td>
            </tr>
                <tr>
                <td>任课教师:</td>
                <td>
                    <input type="text" name="username" />
                </td>
            </tr>
            <tr>
                <td>上课地点:</td>
                <td>
                    <input type="text" name="password" />
                </td>
            </tr>
            <tr align="center">
                <td colspan="2">
                    <input type="submit" value="保存" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

UserDaoImpl.java

package com.jaovo.msg.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;


import com.jaovo.msg.Util.DBUtil;
import com.jaovo.msg.Util.UserException;
import com.jaovo.msg.model.User;

import sun.net.www.content.text.plain;

public class UserDaoImpl implements IUserDao {

    @SuppressWarnings("resource")
    @Override
    public void add(User user) {
        //获得链接对象
        Connection connection = DBUtil.getConnection();
        //准备sql语句
        
        //创建语句传输对象
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {            
            String sql = "insert into t_user(username,password,nickname) values (?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getUsername());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.setString(3, user.getNickname());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            //关闭资源
            DBUtil.close(resultSet);
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
        
    }

    @Override
    public void delete(int id) {
        Connection connection = DBUtil.getConnection();
        String sql = "delete from t_user where id = ?";
        PreparedStatement preparedStatement = null;
        
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
        
    }

    @Override
    public void update(User user) {
        Connection connection = DBUtil.getConnection();
        //准备sql语句
        String sql = "update t_user set password = ? , nickname=? where id = ?";
        //创建语句传输对象
        PreparedStatement preparedStatement = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, user.getPassword());
            preparedStatement.setString(2, user.getNickname());
            preparedStatement.setInt(3, user.getId());
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
    }

    @Override
    public User load(int id) {
        Connection connection = DBUtil.getConnection();
        //准备sql语句
        String sql = "select * from t_user  where id = ?";
        //创建语句传输对象
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        User user = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, id);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()) {
                user = new User();
                user.setId(id);
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setNickname(resultSet.getString("nickname"));
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(resultSet);
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
        return  user;
    }

    @Override
    public User load(String username) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<User> load() {
        Connection connection = DBUtil.getConnection();
        //准备sql语句
        String sql = "select * from t_user ";
        //创建语句传输对象
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        //集合中只能放入user对象
        List<User> users = new ArrayList<User>();
        User user = null;
        try {
            preparedStatement = connection.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()) {
                user = new User();
                user.setId(resultSet.getInt("id"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setNickname(resultSet.getString("nickname"));
                users.add(user);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            DBUtil.close(resultSet);
            DBUtil.close(preparedStatement);
            DBUtil.close(connection);
        }
        return  users;
    }

}

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 {
            //1 加载驱动
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String user = "******";
        String password = "*******";
        String url = "jdbc:sqlserver://localhost:1433;DatabaseName=******";
        Connection connection = null;
        try {
            //2 创建链接对象connection
             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();
        }
    }
    
}

User.java

package com.jaovo.msg.model;

public class User {
    
    private int id;
    private String  username;
    private String  nickname;
    private String  password;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getNickname() {
        return nickname;
    }
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    
}

UserException.java

package com.jaovo.msg.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
    }
    
}

IUserDao.java

package com.jaovo.msg.dao;

import java.util.List;

import com.jaovo.msg.model.User;

public interface IUserDao {
    public void add(User user);
    public void delete(int id);
    public void update(User user);
    public User load(int id);
    public User load(String username);
    public List<User> load();
    
}

三、实验截图

四、项目、时间、缺陷计划日志

以上是关于软件工程概论课堂测试一的主要内容,如果未能解决你的问题,请参考以下文章

软件工程概论课堂测试一

软件工程概论课堂测试一(设计添加新课程界面)

软件工程概论课堂测试一————添加新课程(web)

软件工程概论课堂作业1

软件工程概论-课后作业1

软工概论第一次课堂测试