软件工程课堂练习2

按照图片要求设计添加新课程界面
要求判断任课教师、上课地点。
实现数据存储功能。
设计思想:
用java web实现。
定义教师类UserBean及其属性,连接数据库类DBUtil,数据库操作类UserDao。
用JSP实现界面。
源代码:
UserBean:
package Bean;

public class UserBean {
	private String classname;
	private String teacher;
	private String classroom;
	
	public void setClassName(String classname) {
		this.classname = classname;
	}
	public String getClassName() {
		return classname;
	}
	public void setTeacher(String teacher) {
		this.teacher = teacher;
	}
	public String getTeacher() {
		return teacher;
	}
	public void setClassRoom(String classroom) {
		this.classroom = classroom;
	}
	public String getClassRoom() {
		return classroom;
	}
}

DBUtil:

package util;

import java.sql.*;
public class DBUtil {
	public static Connection getConnection() {
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String username = "sa";
		String password = "luohaochi123";
		String url = "jdbc:sqlserver://localhost:1433;DatabaseName=Teacher";
		Connection connection = null;
		try {
			connection = DriverManager.getConnection(url, username, password);
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		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();
		}
	}
}
UserDao:
package Dao;

import java.sql.*;
import util.DBUtil;
import Bean.UserBean;
import Bean.UserException;
public class UserDao {
	public void add(UserBean user) {
		Connection connection=DBUtil.getConnection();
		String sql = "select * from user1 where teacher = ?";
		PreparedStatement preparedStatement=null;
		ResultSet resultSet=null;
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1,user.getTeacher());
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				if (resultSet.getInt(1) > 0) {
					throw new UserException("错误") ;
				}
			}
			sql = "insert into user1 (classname,teacher,classroom) values(?,?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, user.getClassName());
			preparedStatement.setString(2, user.getTeacher());
			preparedStatement.setString(3, user.getClassRoom());
			preparedStatement.executeUpdate();
		}
		catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
			DBUtil.close(resultSet);
			DBUtil.close(preparedStatement);
			DBUtil.close(connection);
		}
	}
}
输入界面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%response.setCharacterEncoding("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>
   <form action='add.jsp' method ="post">
   <% 
   if(request.getAttribute("error")!=null||"".equals(request.getAttribute("error")))
    {
	   %>
	   <%=request.getAttribute("error") %>
	   <%
    }
   %>
   <% 
      if(request.getAttribute("error1")!=null||"".equals(request.getAttribute("error1")))
    {
	   %>
	   <%=request.getAttribute("error1") %>
	   <%
    }
   %>
    <% 
      if(request.getAttribute("error2")!=null||"".equals(request.getAttribute("error2")))
    {
	   %>
	   <%=request.getAttribute("error2") %>
	   <%
    }
   %>
   <% 
      if(request.getAttribute("error3")!=null||"".equals(request.getAttribute("error3")))
    {
	   %>
	   <%=request.getAttribute("error3") %>
	   <%
    }
   %>
    <% 
      if(request.getAttribute("error4")!=null||"".equals(request.getAttribute("error4")))
    {
	   %>
	   <%=request.getAttribute("error4") %>
	   <%
    }
   %>
   <table align="center" >
        <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="classroom"/>
          </td>
        </tr>
        <tr align="center">
    			<td colspan="2">
    				<input type="submit" value="保存" />
    			</td>
    		</tr>
    		</table>
    		</form>
</body>
</html>
处理界面:
<%@page import="Dao.UserDao"%>
<%@page import="Bean.UserBean"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%response.setCharacterEncoding("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 classname=request.getParameter("classname");
    String teacher=request.getParameter("teacher");
    String classroom=request.getParameter("classroom");
    if(classname==null||"".equals(classname.trim())){
 	   request.setAttribute("error", "课程名称不能为空!");
 	   %>
 	   <jsp:forward page="addinput.jsp"></jsp:forward>
 	<%
    }
    if(teacher==null||"".equals(teacher.trim())){
  	   request.setAttribute("error1", "任课教师不能为空!");
  	   %>
  	   <jsp:forward page="addinput.jsp"></jsp:forward>
  	<%
     }
    if(classroom==null||"".equals(classroom.trim())){
  	   request.setAttribute("error2", "上课地点不能为空!");
  	   %>
  	   <jsp:forward page="addinput.jsp"></jsp:forward>
  	<%
     }
    if(!("王建民".equals(teacher.trim())||"刘立嘉".equals(teacher.trim())||"刘丹".equals(teacher.trim())||"王辉".equals(teacher.trim())||"杨子光".equals(teacher.trim())))
    {
    	request.setAttribute("error3", "任课教师超出范围!");
    	%>
    	<jsp:forward page="addinput.jsp"></jsp:forward>
    	<%
    }
    if(!(classroom.startsWith("基教")||classroom.startsWith("一教")||classroom.startsWith("二教")||classroom.startsWith("三教")))
    {
    	request.setAttribute("error4", "上课地点超出范围!");
    	%>
    	<jsp:forward page="addinput.jsp"></jsp:forward>
    	<%
    }
    UserBean user=new UserBean();
    user.setClassName(classname);
    user.setTeacher(teacher);
    user.setClassRoom(classroom);
    UserDao dao=new UserDao();
    dao.add(user);
    %>
    <jsp:forward page="success.jsp"></jsp:forward><% 
%>
</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>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>保存成功</title>
</head>
<body>
保存成功!!<br>
    <a href="addinput.jsp">继续保存</a><br>
</body>
</html>
截图:

PSP0

PSP0级过程管理包括计划、开发和总结三个阶段,
最终提交经过完全测试的程序和填写完成的项目计划总结表、时间和缺陷记录日志

计划过程管理 
开发过程管理 

总结过程管理 

 

posted @ 2017-11-28 20:31  什么名都不好  阅读(270)  评论(0)    收藏  举报