Yang

Javaweb实现简单的选课系统(主要技术包括jsp、Servlet、Javabean)

一、背景信息:

首先,这个选课系统是上周一老师在课堂上让我们做的测试,考试时长三个小时,我只做了一半,也没有实现选课流程。

由于上周忙于写实验报告没有时间继续完成这个测试。这周用前天和昨天一共七个小时的时间从头重新写了一遍并且完全完成所有要求,实现了选课流程和一系列增加修改操作

二、题目

 

石家庄铁道大学选课管理系统

1、项目需求:

本项目所开发的学生选课系统完成学校对学生的选课信息的统计与管理,减少数据漏掉的情况,同时也节约人力、物力和财力。告别以往的人工统计。

2.系统要求与功能设计

2.1 页面要求

(1)能够在Tomcat服务器中正确部署,并通过浏览器查看;

(2)网站页面整体风格统一;

(3)首页(登录页)要求实现不同用户登录后,进入的功能页不相同。

(4)教师功能页:有添加课程、修改个人信息、浏览选课学生信息三个模块。

(5)学生功能页:有修改个人信息、浏览课程信息、选课三个功能模块。

(5)管理员功能页:有添加教师信息、添加学生信息两个模块。

2.2功能要求:

(1)添加教师信息:管理员可以添加教师基本信息,教师基本信息包括:教师工号(八位数字组成,例如02000081)、教师姓名、教师性别、教师所在学院、职称(教授、副教授、讲师、助教)组成;

(2)添加学生信息:管理可以添加学生基本信息,学生基本信息包括学号(八位数字组成,例如20180052)、学生姓名、学生性别、所在班级、所属专业组成;

(3)添加课程信息:教师登陆后,可以添加自己任职的课程基本信息,课程基本信息包括:课程编号(六位数字组成,例如050013),课程名称、选课人数、任课教师(任课教师不需录入,那位教师填写课程信息,那位教师就是任课教师);

(4)修改个人信息:教师或学生登陆后可以修改个人信息,但教师工号或学号不能修改,另外教师或学生只能修改自己的信息,无法看到或修改其他学生或教师的基本信息。

(5)浏览课程信息:学生登陆后可以看到所有课程的列表信息,点击课程名称可以查看课程的详细信息,包括已选课人数。

(6)选课:进入选课页面,课程信息列表显示所有选课人数未达到课程设置的选课人数上限,点击课程名称可以看到课程详细信息,点击课程详细信息页面的“选课”按钮,可以实现选课功能。

(7)浏览选课学生信息:教师进入该页面后,可以看到自己设置的课程信息列表,点击课程名称,可以看到,选择该课程的所有学生基本信息列表。

(8)登陆功能:管理员、教师、学生登陆后可以看到不同的功能页面,教师或学生登陆后只能看到自己的相关信息,不同教师、不同学生登陆后无法查看其他人的信息。(要求至少创建两个教师用户、十个学生用户演示选课过程)

3数据库设计:

要求实现课程基本信息表、教师基本信息表、学生基本信息表、选课基本信息表。(提示:选课基本信息包括课程编号、教师编号、学号等基本信息)

4、WEB发布:

要求可以实现在浏览器直接访问系统。

 

三、主要思路:

主要是通过jsp获取参数或数据,然后将数据传给servlet,servlet通过Javabean中的类和调用Dao中的方法(主要是SQL语句)处理,

然后将处理后的数据回传给jsp进行显示。

 

四、源代码

1、Eclipse展示

 

 

 

 

 

 

  login.jsp(选课系统登录入口)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
  <table align="center" border="0px" cellpadding="10px" cellspacing="10px">
  <form action="xuankeServlet?method=login"  method="post"  onsubmit="return check()">
  <tr>
  <td>用户名:</td>
  <td><input type="text" name="username" id="username"></td>
  </tr>
  <tr>
  <td>密码:</td>
  <td><input type="password" name="password" id="password"></td>
  </tr>
    <tr align="center">
    <th colspan="2">
    <input type="submit" value="登录">
    </th>
    </tr>
  </form>
  </table>
</body>
</html>

 

guanlileft.jsp(管理员左侧菜单栏)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">

.leftMenu{
    background-color:#7986CB;
    width:250px; 
    height:600px;
    position:relative;
    top:0px;
}

.menuParent{
    background-color:#BBDEFB;
}



</style>
</head>
<body>

<div class="leftMenu">
    <div class="menu">
    
    
        <div class="menuParent">
            <div class="ListTitlePanel">
                <div class="ListTitle">
                    <strong>管理员菜单</strong>                    
                    <div class="leftbgbt"></div>
                </div>
            </div>
            
            <div class="menuList">
                <div><a target="main_right" href="addtea.jsp">添加教师信息</a></div>
                <div><a target="main_right" href="addstu.jsp">添加学生信息</a></div>             
            </div>
        </div>
        
        
    </div>
</div>

<script type="text/javascript">



</script>

</body>
</html>

 

tealeft.jsp(教师左侧菜单栏)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">

.leftMenu{
    background-color:#7986CB;
    width:250px; 
    height:600px;
    position:relative;
    top:0px;
}

.menuParent{
    background-color:#BBDEFB;
}



</style>
</head>
<body>

<div class="leftMenu">
    <div class="menu">
    
    
        <div class="menuParent">
            <div class="ListTitlePanel">
                <div class="ListTitle">
                    <strong>教师菜单</strong>                    
                    <div class="leftbgbt"></div>
                </div>
            </div>
            
            <div class="menuList">
                <div><a target="main_right" href="addcourse.jsp">添加课程信息</a></div>
                <div><a target="main_right" href="updatet.jsp">修改个人信息</a></div>
                <div><a target="main_right" href="xuankeServlet?method=liulanstu">浏览选课学生信息</a></div>               
            </div>
        </div>
        
        
    </div>
</div>

<script type="text/javascript">



</script>

</body>
</html>

 

stuleft.jsp(学生左侧菜单栏)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">

.leftMenu{
    background-color:#7986CB;
    width:250px; 
    height:600px;
    position:relative;
    top:0px;
}

.menuParent{
    background-color:#BBDEFB;
}



</style>
</head>
<body>

<div class="leftMenu">
    <div class="menu">
    
    
        <div class="menuParent">
            <div class="ListTitlePanel">
                <div class="ListTitle">
                    <strong>学生菜单</strong>                    
                    <div class="leftbgbt"></div>
                </div>
            </div>
            
            <div class="menuList">
            <div><a target="main_right" href="xuankeServlet?method=liulankecheng">选课</a></div> 
                <div><a target="main_right" href="updates.jsp">修改个人信息</a></div>
                <div><a target="main_right" href="xuankeServlet?method=liulankecheng1">浏览课程信息</a></div>
                              
            </div>
        </div>
        
        
    </div>
</div>

<script type="text/javascript">



</script>

</body>
</html>

 

addstu.jsp(添加学生信息)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加学生信息</title>
<script>
</script>
</head>
<body>
<%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
  <table align="center" border="0px" cellpadding="10px" cellspacing="10px">
  <form action="xuankeServlet?method=addstu"  method="post"  onsubmit="return check()">
  <tr>
  <td><a href="3.jsp">返回菜单</a></td>
  </tr>
   <tr>
    <td>学号:</td>
    <td><input type="text" name="spid" id="spid"></td>
   </tr>
   <tr>
   <td>姓名:</td>
   <td><input type="text" name="stuname" id="stuname"></td>
   </tr>
   <tr>
   <td>性别</td>
   <td>
   <select name="sex">
   <option value="男">男</option>
   <option value="女">女</option>
    </select>
   </td>
   </tr>
   
   <tr>
   <td>学生所在班级:</td>
   <td><input type="text" name="banji" id="banji"></td>
   </tr>
   <tr>
   <td>所属专业:</td>
   <td><input type="text" name="ye" id="ye"></td>
   </tr>
 
    <tr align="center">
    <th colspan="2">
    <input type="submit" value="提交">
    </th>
    </tr>
    </form>
</table>
</body>
</html>

 

 

addtea.jsp(添加教师信息)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加教师信息</title>
<script>
</script>
</head>
<body>
<%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
  <table align="center" border="0px" cellpadding="10px" cellspacing="10px">
  <form action="xuankeServlet?method=addtea"  method="post"  onsubmit="return check()">
  <tr>
  <td><a href="3.jsp">返回菜单</a></td>
  </tr>
   <tr>
    <td>教师工号:</td>
    <td><input type="text" name="tpid" id="tpid"></td>
   </tr>
   <tr>
   <td>教师姓名:</td>
   <td><input type="text" name="teaname" id="teaname"></td>
   </tr>
   <tr>
   <td>性别</td>
   <td>
   <select name="sex">
   <option value="男">男</option>
   <option value="女">女</option>
    </select>
   </td>
   </tr>
   
   <tr>
   <td>教师所在学院:</td>
   <td><input type="text" name="txueyuan" id="txueyuan"></td>
   </tr>
   <tr>
   <td>职称:</td>
   <td>
    <select name="zhicheng">
    <option value="教授">教授</option>
    <option value="副教授">副教授</option>
    <option value="讲师">讲师</option>
    <option value="助教">助教</option>
    </select>
    </td>
   </tr>
 
    <tr align="center">
    <th colspan="2">
    <input type="submit" value="提交">
    </th>
    </tr>
    </form>
</table>
</body>
</html>

 

 

addcourse.jsp(添加课程信息)

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加学生信息</title>
<script>
</script>
</head>
<body>
<%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
  <table align="center" border="0px" cellpadding="10px" cellspacing="10px">
  <form action="xuankeServlet?method=addcou"  method="post"  onsubmit="return check()">
  <tr>
  <td><a href="1.jsp">返回菜单</a></td>
  </tr>
   <tr>
    <td>课程编号:</td>
    <td><input type="text" name="cpid" id="cpid"></td>
   </tr>
   <tr>
   <td>课程名称:</td>
   <td><input type="text" name="cname" id="cname"></td>
   </tr>
 
   <tr>
   <td>选课人数:</td>
   <td><input type="text" name="num" id="num"></td>
   </tr>
    <tr>
    
    <tr align="center">
    <th colspan="2">
    <input type="submit" value="提交">
    </th>
    </tr>
    </form>
</table>
</body>
</html>

 

 

houtai.jsp(这个jsp主要是不同用户登录,跳转到不同主界面)(主界面就是左侧菜单栏,上部是一个菜单栏,右侧是一个空白的显示框)

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>菜单</title>
</head>
<body>
  <a  href="${pid}.jsp">菜单</a>
</body>
</html>

 

 

1.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>
<frameset rows="20%,*">
<frame class="top" src="top.jsp">
 <frameset cols="20%,*">
<frame src="tealeft.jsp">
 <frame src="main_right.jsp" name="main_right">
 </frameset>
 </frameset>
 <body>
 
 </body>
 </html>

 

2.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>
<frameset rows="20%,*">
<frame class="top" src="top.jsp">
 <frameset cols="20%,*">
<frame src="stuleft.jsp">
 <frame src="main_right.jsp" name="main_right">
 </frameset>
 </frameset>
 <body>
 
 </body>
 </html>

 

3.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>
<frameset rows="20%,*">
<frame class="top" src="top.jsp">
 <frameset cols="20%,*">
<frame src="guanlileft.jsp">
 <frame src="main_right.jsp" name="main_right">
 </frameset>
 </frameset>
 <body>
 
 </body>
 </html>

 

 

top.jsp(头部菜单栏)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">

.headdiv{
    background-color:#3F51B5;
    width:100%; 
    height:130px;
}

.headdiv P{
    font-family:YouYuan;
    font-size:20px;
    color:#E8EAF6;
    position:relative;
    left:20px;
    top:45px;
}

</style>
</head>
<body>

<div class="headdiv">
    <p>选课系统</p>
</div>

</body>
</html>

 

main_right.jsp(右侧空白显示框)

  

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>

</body>
</html>

 

chakankecheng.jsp(显示课程详细信息)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>课程信息详情</title>
<style>
    .a{
        margin-top: 20px;
    }
    .b{
        font-size: 20px;
        width: 160px;
        color: white;
        background-color: greenyellow;
    }
    .tb, td {
        border: 0px solid black;
        font-size: 22px;
    }
</style>
</head>

<body>

    <div align="center">
        <h1 style="color: black;">课程详细信息</h1>
        <table class="tb">
        <tr>
        <td><a href="2.jsp">返回菜单</a></td>
        </tr>
            <tr>
                <td>课程编号</td>
                <td>课程名称</td>
                <td>课程容量</td>
                <td>课程选课人数</td>
                <td>任课教师</td> 
            </tr>
                <tr>
                    <td>${cou.cpid}</td>
                    <td>${cou.cname}</td>
                    <td>${cou.num}</td>
                    <td>${cou.snum}</td>
                    <td>${cou.jiaoshi}</td>
                </tr>
        </table>
        
     <table align="center" border="0px" cellpadding="10px" cellspacing="10px">
   <form action="xuankeServlet?method=xuanke"  method="post"  onsubmit="return check()">
   <tr>
    <td><input type="hidden" name="cpid" id="cpid" value="${cou.cpid}"></td>
   </tr>
   
   <tr>
   <td><input type="hidden" name="snum" id="snum" value="${cou.snum}"></td>
   </tr>
 
   <tr>
   <td><input type="hidden" name="jiaoshi" id="jiaoshi" value="${cou.jiaoshi}"></td>
   </tr>
    
    <tr align="center">
    <th colspan="2">
    <input type="submit" value="选课">
    </th>
    </tr>
    </form>
    </table> 
    
    </div>

</body>
</html>

 

liulankecheng.jsp(只显示课程名,且课程名带超链接的列表信息)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>

</head>
<body>
    <%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
    <div align="center">
        <h1 style="color: black;">课程信息列表</h1>
        <table class="tb">
            <tr>
                <td>&nbsp;&nbsp;课程名称&nbsp;&nbsp;</td>
            </tr>
            <c:forEach items="${cous}" var="item">
                <tr>
                    <td>&nbsp;&nbsp;&nbsp;&nbsp;<a href="xuankeServlet?method=chakan&id=${item.id}">${item.cname}</a></td>
                </tr>
            </c:forEach>
        </table>
    </div>
</body>
</html>

 

liulankecheng1.jsp(显示所有课程的信息信息)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>

</head>
<body>
    <%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
    <div align="center">
        <h1 style="color: black;">课程信息列表</h1>
        <table class="tb">
            <tr>
        <td><a href="2.jsp">返回菜单</a></td>
        </tr>
            <tr>
                <td>课程编号</td>
                <td>课程名称</td>
                <td>课程容量</td>
                <td>课程选课人数</td>
                <td>任课教师</td> 
            </tr>
            <c:forEach items="${cous}" var="item">
                <tr>
                    <td>${item.cpid}</td>
                    <td>${item.cname}</td>
                    <td>${item.num}</td>
                    <td>${item.snum}</td>
                    <td>${item.jiaoshi}</td>
                </tr>
            </c:forEach>
        </table>
    </div>
</body>
</html>

 

liulanstu.jsp(浏览选课学生信息)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>

</head>
<body>
    <%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
    <div align="center">
        <h1 style="color: black;">课程信息列表</h1>
        <table class="tb">
            <tr>
        <td><a href="2.jsp">返回菜单</a></td>
        </tr>
            <tr>
                <td>学生学号</td>
                <td>学生姓名</td>
                <td>学生性别</td>
                <td>学生所在班级</td>
                <td>学生专业</td> 
            </tr>
            <c:forEach items="${jibens}" var="item">
                <tr>
                    <td>${item.spid}</td>
                    <td>${item.stuname}</td>
                    <td>${item.sex}</td>
                    <td>${item.banji}</td>
                    <td>${item.ye}</td>
                </tr>
            </c:forEach>
        </table>
    </div>
</body>
</html>

 

sucess.jsp(选课成功后的界面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
    <%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
    
   <table align="center" border="0px" cellpadding="10px" cellspacing="10px">
   <tr>
  <td><a href="xuankeServlet?method=liulankecheng">返回选课</a></td>
  </tr>
   </table>
</body>
</html>

 

updatet.jsp(修改教师信息)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改教师信息</title>
<script>
</script>
</head>
<body>
<%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
  <table align="center" border="0px" cellpadding="10px" cellspacing="10px">
  <form action="xuankeServlet?method=updatetea"  method="post"  onsubmit="return check()">
  <tr>
  <td><a href="1.jsp">返回菜单</a></td>
  </tr>
   
   <tr>
   <td>教师姓名:</td>
   <td><input type="text" name="teaname" id="teaname"></td>
   </tr>
   <tr>
   <td>性别</td>
   <td>
   <select name="sex">
   <option value="男">男</option>
   <option value="女">女</option>
    </select>
   </td>
   </tr>
   
   <tr>
   <td>教师所在学院:</td>
   <td><input type="text" name="txueyuan" id="txueyuan"></td>
   </tr>
   <tr>
   <td>职称:</td>
   <td>
    <select name="zhicheng">
    <option value="教授">教授</option>
    <option value="副教授">副教授</option>
    <option value="讲师">讲师</option>
    <option value="助教">助教</option>
    </select>
    </td>
   </tr>
 
    <tr align="center">
    <th colspan="2">
    <input type="submit" value="提交">
    </th>
    </tr>
    </form>
</table>
</body>
</html>

 

updates.jsp(修改学生信息)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改学生信息</title>
<script>
</script>
</head>
<body>
<%
         Object message = request.getAttribute("message");
         if(message!=null && !"".equals(message)){
     
    %>
         <script type="text/javascript">
              alert("<%=request.getAttribute("message")%>");
         </script>
    <%} %>
  <table align="center" border="0px" cellpadding="10px" cellspacing="10px">
  <form action="xuankeServlet?method=updatestu"  method="post"  onsubmit="return check()">
  <tr>
  <td><a href="2.jsp">返回菜单</a></td>
  </tr>
   <tr>
   <td>姓名:</td>
   <td><input type="text" name="stuname" id="stuname"></td>
   </tr>
   <tr>
   <td>性别</td>
   <td>
   <select name="sex">
   <option value="男">男</option>
   <option value="女">女</option>
    </select>
   </td>
   </tr>
   
   <tr>
   <td>学生所在班级:</td>
   <td><input type="text" name="banji" id="banji"></td>
   </tr>
   <tr>
   <td>所属专业:</td>
   <td><input type="text" name="ye" id="ye"></td>
   </tr>
 
    <tr align="center">
    <th colspan="2">
    <input type="submit" value="提交">
    </th>
    </tr>
    </form>
</table>
</body>
</html>

 

Servlet:

xuankeServlet.java

package xuankeServlet;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.Dao;
import Javabean.Cou;
import Javabean.Jibenxinxi;
import Javabean.Stu;
import Javabean.Teacher;




@WebServlet("/xuankeServlet")
public class xuankeServlet extends HttpServlet{

    /**
     * 特有id号
     */
    private static final long serialVersionUID = 1L;
    Dao dao = new Dao();
    /**
     * 方法选择
     * @return 
     * @throws IOException 
     * @throws ServletException 
     */
    protected void service(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException
    {
        req.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        if("login".equals(method)) {
            login(req,resp);
        }else if("addtea".equals(method)) {
            addtea(req,resp);
        }else if("addstu".equals(method)) {
            addstu(req,resp);
        }else if("addcou".equals(method)) {
            addcou(req,resp);
        }else if("updatetea".equals(method)) {
            updatetea(req,resp);
        }else if("updatestu".equals(method)) {
            updatestu(req,resp);
        }else if("liulankecheng".equals(method)) {
            liulankecheng(req,resp);
        }else if("chakan".equals(method)) {
            chakan(req,resp);
        }else if("xuanke".equals(method)) {
            xuanke(req,resp);
        }else if("liulankecheng1".equals(method)) {
            liulankecheng1(req,resp);
        }else if("liulanstu".equals(method)) {
            liulanstu(req,resp);
        }
    }
  
    private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
        req.setCharacterEncoding("utf-8");
        String username = req.getParameter("username");
        String password=req.getParameter("password");
        int pid=dao.searchPid(username,password);
        System.out.println(pid);
        if(pid==1) {
            String teaname=dao.searchteaname(username);
            System.out.println(teaname);
            req.getSession().setAttribute("username", username);
            req.getSession().setAttribute("teaname", teaname);
        }
        if(pid==2) {
            req.getSession().setAttribute("username1", username);
        }
        req.setAttribute("pid", pid);
        req.getRequestDispatcher("houtai.jsp").forward(req, resp);
        }
    private void addtea(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String tpid = req.getParameter("tpid");
        String teaname = req.getParameter("teaname");
        String sex = req.getParameter("sex");
        String txueyuan = req.getParameter("txueyuan");
        String zhicheng = req.getParameter("zhicheng");
        int pid=1;
        String password = "123456";
        Teacher teacher=new Teacher(tpid,teaname,sex,txueyuan,zhicheng);
        if(dao.addtea(teacher)&&dao.adduser(tpid,password,pid)) {
            req.setAttribute("teacher",teacher);
            req.setAttribute("message","添加成功" );
            req.getRequestDispatcher("addtea.jsp").forward(req, resp);
        }else {
            req.setAttribute("message","老师姓名重复,请重新输入" );
            req.getRequestDispatcher("addtea.jsp").forward(req, resp);
        }
    }
    
    private void addstu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String spid = req.getParameter("spid");
        String stuname = req.getParameter("stuname");
        String sex = req.getParameter("sex");
        String banji = req.getParameter("banji");
        String ye = req.getParameter("ye");
        Stu stu=new Stu(spid,stuname,sex,banji,ye);
        int pid=2;
        String password="123456";
        if(dao.addstu(stu)&&dao.adduser(spid, password, pid)) {
            req.setAttribute("stu",stu);
            req.setAttribute("message","添加成功" );
            req.getRequestDispatcher("addstu.jsp").forward(req, resp);
        }else {
            req.setAttribute("message","学生姓名重复,请重新输入" );
            req.getRequestDispatcher("addstu.jsp").forward(req, resp);
        }
    }

    private void addcou(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String jiaoshi=(String) req.getSession().getAttribute("teaname");
        String cpid = req.getParameter("cpid");
        String cname = req.getParameter("cname");
        int num = Integer.parseInt(req.getParameter("num"));
        int snum=0;
        System.out.println(jiaoshi);
        Cou cou=new Cou(cpid,cname,num,snum,jiaoshi);
        if(dao.addcou(cou)) {
            req.setAttribute("cou",cou);
            req.setAttribute("message","添加成功" );
            req.getRequestDispatcher("addcourse.jsp").forward(req, resp);
        }else {
            req.setAttribute("message","课程信息重复,请重新输入" );
            req.getRequestDispatcher("addcourse.jsp").forward(req, resp);
        }
    }
    private void updatetea(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String tpid=(String) req.getSession().getAttribute("username");
        String teaname = req.getParameter("teaname");
        String sex = req.getParameter("sex");
        String txueyuan = req.getParameter("txueyuan");
        String zhicheng = req.getParameter("zhicheng");
        Teacher teacher=new Teacher(tpid,teaname,sex,txueyuan,zhicheng);
        if(dao.updatetea(teacher)) {
            req.setAttribute("message","修改成功" );
            req.getRequestDispatcher("updatet.jsp").forward(req, resp);    
        } else {
            req.setAttribute("message","修改失败" );
            req.getRequestDispatcher("updatet.jsp").forward(req, resp);
        }
    }
    
    private void updatestu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String spid =(String) req.getSession().getAttribute("username1");
        String stuname = req.getParameter("stuname");
        String sex = req.getParameter("sex");
        String banji = req.getParameter("banji");
        String ye = req.getParameter("ye");
        Stu stu=new Stu(spid,stuname,sex,banji,ye);
        if(dao.updatestu(stu)) {
            req.setAttribute("message","修改成功" );
            req.getRequestDispatcher("updates.jsp").forward(req, resp);    
        } else {
            req.setAttribute("message","修改失败" );
            req.getRequestDispatcher("updates.jsp").forward(req, resp);
        }
    }
    
    
    private void liulankecheng(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        List<Cou> cous = dao.liulankecheng();
        req.setAttribute("cous", cous);
        req.getRequestDispatcher("liulankecheng.jsp").forward(req, resp);
    
    }
    
    private void chakan(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        int id = Integer.parseInt(req.getParameter("id"));
        Cou cou=dao.searchByid(id);
        req.getSession().setAttribute("id", id);
        req.setAttribute("cou", cou);
        req.getRequestDispatcher("chakankecheng.jsp").forward(req, resp);
    }
    
    private void xuanke(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String spid =(String) req.getSession().getAttribute("username1");
        String cpid = req.getParameter("cpid");
        int snum = Integer.parseInt(req.getParameter("snum"))+1;
        String jiaoshi = req.getParameter("jiaoshi");
        int id =(int) req.getSession().getAttribute("id");
        String tpid=dao.searchtpid(jiaoshi);
        Stu stu = dao.searchstu(spid);
        System.out.println(id);
        if(dao.updatecou(id,snum)&&dao.addjiben(cpid,tpid,spid,stu)) {
            req.setAttribute("message","选课成功" );
            req.getRequestDispatcher("sucess.jsp").forward(req, resp);
        } else {
            req.setAttribute("message","选课失败" );
            req.getRequestDispatcher("sucess.jsp").forward(req, resp);
        }
    }
    
    private void liulankecheng1(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        List<Cou> cous = dao.liulankecheng1();
        req.setAttribute("cous", cous);
        req.getRequestDispatcher("liulankecheng1.jsp").forward(req, resp);
    
    }
    
    private void liulanstu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String tpid=(String) req.getSession().getAttribute("username");
        List<Jibenxinxi> jibens = dao.searchstuinfo(tpid);
        req.setAttribute("jibens",jibens);
        req.getRequestDispatcher("liulanstu.jsp").forward(req, resp);
    }
    
}

 

Javabean:

Cou.java (课程信息类)

package Javabean;

public class Cou {
    private int id;
    private String cpid;
    private String cname;
    private int num;
    private int snum;
    private String jiaoshi;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getCpid() {
        return cpid;
    }
    public void setCpid(String cpid) {
        this.cpid = cpid;
    }
    public String getCname() {
        return cname;
    }
    public void setCname(String cname) {
        this.cname = cname;
    }
    public int getNum() {
        return num;
    }
    public void setNum(int num) {
        this.num = num;
    }
    public int getSnum() {
        return snum;
    }
    public void setSnum(int snum) {
        this.snum = snum;
    }
    public String getJiaoshi() {
        return jiaoshi;
    }
    public void setJiaoshi(String jiaoshi) {
        this.jiaoshi = jiaoshi;
    }
    public Cou() {}
    public Cou(int id,String cpid,String cname,int num,int snum,String jiaoshi) {
        this.id=id;
        this.cpid=cpid;
        this.cname=cname;
        this.num=num;
        this.snum=snum;
        this.jiaoshi=jiaoshi;
    }
    public Cou(String cpid,String cname,int num,int snum,String jiaoshi) {
        this.cpid=cpid;
        this.cname=cname;
        this.num=num;
        this.snum=snum;
        this.jiaoshi=jiaoshi;
    }
}

 

Teacher.java (教师信息类)

package Javabean;

public class Teacher {

    private int id;
    private String tpid;
    private String teaname;
    private String sex;
    private String txueyuan;
    private String zhicheng;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getTpid() {
        return tpid;
    }
    public void setTpid(String tpid) {
        this.tpid = tpid;
    }
    public String getTeaname() {
        return teaname;
    }
    public void setTeaname(String teaname) {
        this.teaname = teaname;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getTxueyuan() {
        return txueyuan;
    }
    public void setTxueyuan(String txueyuan) {
        this.txueyuan = txueyuan;
    }
    public String getZhicheng() {
        return zhicheng;
    }
    public void setZhicheng(String zhicheng) {
        this.zhicheng = zhicheng;
    }
    public Teacher() { }
    public Teacher(int id,String tpid,String teaname,String sex,String txueyuan,String zhicheng) {
        this.id=id;
        this.tpid=tpid;
        this.teaname=teaname;
        this.sex=sex;
        this.txueyuan=txueyuan;
        this.zhicheng=zhicheng;
    }
    public Teacher(String tpid,String teaname,String sex,String txueyuan,String zhicheng) {
        this.tpid=tpid;
        this.teaname=teaname;
        this.sex=sex;
        this.txueyuan=txueyuan;
        this.zhicheng=zhicheng;
    }
}

 

Stu.java (学生信息类)

package Javabean;

public class Stu {
 
    private int id;
    private String spid;
    private String stuname;
    private String sex;
    private String banji;
    private String ye;
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getSpid() {
        return spid;
    }
    public void setSpid(String spid) {
        this.spid = spid;
    }
    public String getStuname() {
        return stuname;
    }
    public void setStuname(String stuname) {
        this.stuname = stuname;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getBanji() {
        return banji;
    }
    public void setBanji(String banji) {
        this.banji = banji;
    }
    public String getYe() {
        return ye;
    }
    public void setYe(String ye) {
        this.ye = ye;
    }
    public Stu() { }
    
    public Stu(int id,String spid,String stuname,String sex,String banji,String ye) {
        this.id=id;
        this.spid=spid;
        this.stuname=stuname;
        this.sex=sex;
        this.banji=banji;
        this.ye=ye;
    }
    public Stu(String spid,String stuname,String sex,String banji,String ye) {
        this.spid=spid;
        this.stuname=stuname;
        this.sex=sex;
        this.banji=banji;
        this.ye=ye;
    }
    
}

 

Jibenxinxi.java(选课学生信息类)

package Javabean;

public class Jibenxinxi {

    private int id;
    private String cpid;
    private String tpid;
    private String spid;
    private String stuname;
    private String sex;
    private String banji;
    private String ye;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getCpid() {
        return cpid;
    }
    public void setCpid(String cpid) {
        this.cpid = cpid;
    }
    public String getTpid() {
        return tpid;
    }
    public void setTpid(String tpid) {
        this.tpid = tpid;
    }
    public String getSpid() {
        return spid;
    }
    public void setSpid(String spid) {
        this.spid = spid;
    }
    public String getStuname() {
        return stuname;
    }
    public void setStuname(String stuname) {
        this.stuname = stuname;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public String getBanji() {
        return banji;
    }
    public void setBanji(String banji) {
        this.banji = banji;
    }
    public String getYe() {
        return ye;
    }
    public void setYe(String ye) {
        this.ye = ye;
    }
    public Jibenxinxi() { }
    public Jibenxinxi(int id,String cpid,String tpid,String spid,String stuname,String sex,String banji,String ye) { 
        this.id=id;
        this.cpid=cpid;
        this.tpid=tpid;
        this.spid=spid;
        this.stuname=stuname;
        this.sex=sex;
        this.banji=banji;
        this.ye=ye;
    }
    public Jibenxinxi(String cpid,String tpid,String spid,String stuname,String sex,String banji,String ye) { 
        this.cpid=cpid;
        this.tpid=tpid;
        this.spid=spid;
        this.stuname=stuname;
        this.sex=sex;
        this.banji=banji;
        this.ye=ye;
    }
}

 

Dao:

Dao.java(进行与数据库的交互,主要是SQL语句)

package Dao;

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

import DBUtil.DBUtil;
import Javabean.Cou;
import Javabean.Jibenxinxi;
import Javabean.Stu;
import Javabean.Teacher;

public class Dao {

    public  int searchPid(String username,String password)
       {
             Connection conn = DBUtil.getConn();
             Statement state = null;
             ResultSet rs = null;
             int pid=0;
             try {
                String sql="select pid from user where username= '"+username+"' and password='"+password+"'";
                state = conn.createStatement();
                rs = state.executeQuery(sql);
                while(rs.next()){
                pid = rs.getInt("pid");
                }
             }
             catch(SQLException e) {
                 e.printStackTrace();
             }
             finally {
                 DBUtil.close(state, conn); 
             }
              return pid;
       }
    public boolean addtea(Teacher teacher)
       {
             Connection conn = DBUtil.getConn();
             PreparedStatement pstmt = null;
             boolean f = false;
             int a=0;
             try {
                 String sql = "insert into teacher(tpid,teaname,sex,txueyuan,zhicheng) value(?,?,?,?,?)";
                 pstmt = conn.prepareStatement(sql);
                 pstmt.setString(1, teacher.getTpid());
                 pstmt.setString(2, teacher.getTeaname());
                 pstmt.setString(3, teacher.getSex());
                 pstmt.setString(4, teacher.getTxueyuan());
                 pstmt.setString(5, teacher.getZhicheng());
                a = pstmt.executeUpdate();
             }
             catch(SQLException e) {
                 e.printStackTrace();
             }
             finally {
                 DBUtil.close(pstmt, conn); 
             }
             if(a>0)
             f=true;
             
             return f;
       }
    public boolean adduser(String tpid,String password,int pid)
       {
             Connection conn = DBUtil.getConn();
             PreparedStatement pstmt = null;
             boolean f = false;
             int a=0;
             try {
                 String sql = "insert into user(username,password,pid) value(?,?,?)";
                 pstmt = conn.prepareStatement(sql);
                 pstmt.setString(1, tpid);
                 pstmt.setString(2, password);
                 pstmt.setInt(3, pid);
                a = pstmt.executeUpdate();
             }
             catch(SQLException e) {
                 e.printStackTrace();
             }
             finally {
                 DBUtil.close(pstmt, conn); 
             }
             if(a>0)
             f=true;
             
             return f;
       }
    public boolean addstu(Stu stu)
       {
             Connection conn = DBUtil.getConn();
             PreparedStatement pstmt = null;
             boolean f = false;
             int a=0;
             try {
                 String sql = "insert into stu(spid,stuname,sex,banji,ye) value(?,?,?,?,?)";
                 pstmt = conn.prepareStatement(sql);
                 pstmt.setString(1, stu.getSpid());
                 pstmt.setString(2, stu.getStuname());
                 pstmt.setString(3, stu.getSex());
                 pstmt.setString(4, stu.getBanji());
                 pstmt.setString(5, stu.getYe());
                a = pstmt.executeUpdate();
             }
             catch(SQLException e) {
                 e.printStackTrace();
             }
             finally {
                 DBUtil.close(pstmt, conn); 
             }
             if(a>0)
             f=true;
             
             return f;
       }
    
    public  String searchteaname(String tpid)
       {
             Connection conn = DBUtil.getConn();
             Statement state = null;
             ResultSet rs = null;
             String teaname=null;
             try {
                String sql="select teaname from teacher where tpid= '"+tpid+"'";
                state = conn.createStatement();
                rs = state.executeQuery(sql);
                while(rs.next()){
                teaname = rs.getString("teaname");
                }
             }
             catch(SQLException e) {
                 e.printStackTrace();
             }
             finally {
                 DBUtil.close(state, conn); 
             }
              return teaname;
       }
    public boolean addcou(Cou cou)
       {
             Connection conn = DBUtil.getConn();
             PreparedStatement pstmt = null;
             boolean f = false;
             int a=0;
             try {
                 String sql = "insert into cou(cpid,cname,num,snum,jiaoshi) value(?,?,?,?,?)";
                 pstmt = conn.prepareStatement(sql);
                 pstmt.setString(1, cou.getCpid());
                 pstmt.setString(2, cou.getCname());
                 pstmt.setInt(3, cou.getNum());
                 pstmt.setInt(4, cou.getSnum());
                 pstmt.setString(5, cou.getJiaoshi());
                a = pstmt.executeUpdate();
             }
             catch(SQLException e) {
                 e.printStackTrace();
             }
             finally {
                 DBUtil.close(pstmt, conn); 
             }
             if(a>0)
             f=true;
             
             return f;
       }
    public boolean updatetea(Teacher teacher) {
        String sql = "update teacher set teaname='" + teacher.getTeaname() + "', sex='" + teacher.getSex()
        + "', txueyuan='" +teacher.getTxueyuan() + "',zhicheng='" +teacher.getZhicheng() + "'where tpid='" + teacher.getTpid() + "'";
    Connection conn = DBUtil.getConn();
    Statement state = null;
    boolean f = false;
    int a = 0;
    try {
        state = conn.createStatement();
        System.out.println("看看是不是执行了");
        a = state.executeUpdate(sql);
        System.out.println(a);
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DBUtil.close(state, conn);
    }
    
    if (a > 0) {
        f = true;
    }
    System.out.println(f);
    return f;
    }
    
    public boolean updatestu(Stu stu) {
        String sql = "update stu set stuname='" + stu.getStuname() + "', sex='" + stu.getSex()
        + "', banji='" +stu.getBanji() + "',ye='" +stu.getYe() + "'where spid='" + stu.getSpid() + "'";
    Connection conn = DBUtil.getConn();
    Statement state = null;
    boolean f = false;
    int a = 0;
    try {
        state = conn.createStatement();
        System.out.println("看看是不是执行了");
        a = state.executeUpdate(sql);
        System.out.println(a);
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        DBUtil.close(state, conn);
    }
    
    if (a > 0) {
        f = true;
    }
    
    System.out.println(f);
    return f;
    }
    
    public List<Cou> liulankecheng() {
        String sql = "select * from cou";
        List<Cou> list = new ArrayList<>();
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;

        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            Cou bean = null;
            while (rs.next()) {
                int id = rs.getInt("id");
                String cpid = rs.getString("cpid");
                String cname = rs.getString("cname");
                int num = rs.getInt("num");
                int snum = rs.getInt("snum");
                String jiaoshi = rs.getString("jiaoshi");
                bean = new Cou(id,cpid, cname, num ,snum,jiaoshi);
                list.add(bean);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
        
        return list;
    }
    
    public Cou searchByid(int id) {
        String sql = "select * from cou where id= '"+id+"'";
        Cou cou = null;
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;

        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {
                //int id = rs.getInt("id");
                String cpid = rs.getString("cpid");
                String cname = rs.getString("cname");
                int num = rs.getInt("num");
                int snum = rs.getInt("snum");
                String jiaoshi = rs.getString("jiaoshi");
                cou = new Cou(cpid, cname, num ,snum,jiaoshi);
                
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
        
        return cou;
    }
    public boolean updatecou(int id,int snum) {
        String sql="update cou set snum='" + snum + "'where id='" + id +"'";
        Connection conn = DBUtil.getConn();
        Statement state = null;
        boolean f = false;
        int a = 0;
        try {
            state = conn.createStatement();
            System.out.println("看看是不是执行了");
            a = state.executeUpdate(sql);
            System.out.println(a);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(state, conn);
        }
        
        if (a > 0) {
            f = true;
        }
        
        System.out.println(f);
        return f;
        
        }
    
    public String searchtpid(String jiaoshi) {
        String sql = "select tpid from teacher where teaname= '"+jiaoshi+"'";
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;
        String tpid=null;
        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {
            tpid=rs.getString("tpid");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
        
        return tpid;
    }
    
    public Stu searchstu(String spid) {
        String sql = "select * from stu where spid= '"+spid+"'";
        Stu stu = null;
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;

        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            while (rs.next()) {
                //int id = rs.getInt("id");
                String spid1 = rs.getString("spid");
                String stuname = rs.getString("stuname");
                String sex = rs.getString("sex");
                String banji = rs.getString("banji");
                String ye = rs.getString("ye");
                stu = new Stu(spid1, stuname, sex ,banji,ye);
                
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
        
        return stu;
    }
    
    public boolean addjiben(String cpid,String tpid,String spid,Stu stu) {
        Connection conn = DBUtil.getConn();
        PreparedStatement pstmt = null;
        boolean f = false;
        int a=0;
        try {
            String sql = "insert into jiben(cpid,tpid,spid,stuname,sex,banji,ye) value(?,?,?,?,?,?,?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, cpid);
            pstmt.setString(2, tpid);
            pstmt.setString(3, spid);
            pstmt.setString(4, stu.getStuname());
            pstmt.setString(5, stu.getSex());
            pstmt.setString(6, stu.getBanji());
            pstmt.setString(7, stu.getYe());
           a = pstmt.executeUpdate();
        }
        catch(SQLException e) {
            e.printStackTrace();
        }
        finally {
            DBUtil.close(pstmt, conn); 
        }
        if(a>0)
        f=true;
        
        return f;
    }
    public List<Cou> liulankecheng1() {
        String sql = "select * from cou";
        List<Cou> list = new ArrayList<>();
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;

        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            Cou bean = null;
            while (rs.next()) {
                int id = rs.getInt("id");
                String cpid = rs.getString("cpid");
                String cname = rs.getString("cname");
                int num = rs.getInt("num");
                int snum = rs.getInt("snum");
                String jiaoshi = rs.getString("jiaoshi");
                if(snum<num) {
                bean = new Cou(id,cpid, cname, num ,snum,jiaoshi);
                list.add(bean);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
        
        return list;
    }
    
    public List<Jibenxinxi> searchstuinfo(String tpid) {
        String sql = "select * from jiben where tpid= '"+tpid+"'";
        Connection conn = DBUtil.getConn();
        Statement state = null;
        ResultSet rs = null;
        List<Jibenxinxi> list = new ArrayList<>();

        try {
            state = conn.createStatement();
            rs = state.executeQuery(sql);
            Jibenxinxi jiben = null;
            while (rs.next()) {
                //int id = rs.getInt("id");
                String cpid = rs.getString("cpid");
                String tpid1 = rs.getString("tpid");
                String spid = rs.getString("spid");
                String stuname = rs.getString("stuname");
                String sex = rs.getString("sex");
                String banji = rs.getString("banji");
                String ye = rs.getString("ye");
                jiben = new Jibenxinxi(cpid,tpid1,spid,stuname,sex ,banji,ye);
                list.add(jiben);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, state, conn);
        }
        
        return list;
    }
}

 

DBUtil:

DBUtil.java (获取数据库连接和释放与数据库的连接)

package DBUtil;

import java.sql.*;

/**
 * 数据库连接工具
 * @author Hu
 *
 */
public class DBUtil {
    
    public static String url =  "jdbc:mysql://localhost:3306/xuanke?serverTimezone=UTC";
    public static String user = "root";
    public static String password = "123";
    
    public static Connection getConn () {
        Connection conn = null;
        
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");//加载驱动
            conn = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return conn;
    }
    
    /**
     * 关闭连接
     * @param state
     * @param conn
     */
    public static void close (PreparedStatement preparedState, Connection conn) {
        if (preparedState != null) {
            try {
                preparedState.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void close (ResultSet rs, PreparedStatement preparedState, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (preparedState != null) {
            try {
                preparedState.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 关闭连接
     * @param state
     * @param conn
     */
    public static void close (Statement state, Connection conn) {
        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void close (ResultSet rs, Statement state, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (state != null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void main(String[] args) throws SQLException {
        Connection conn = getConn();
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;
        String sql ="select * from xuanke";
        preparedStatement = conn.prepareStatement(sql);
        rs = preparedStatement.executeQuery();
        if(rs.next()){
            System.out.println("数据库为空");
        }
        else{
            System.out.println("数据库不为空");
        }
    }
}

 

架包:

 

 

 

四、数据库设计

数据库展示:

 

 

 

 

cou表设计(课程信息表)

 

 

 

 

 

jiben表设计(选课学生信息表)

 

 

 

 

stu表设计(学生信息表)

 

 

 

 

teacher表设计(教师信息表)

 

 

 

 

user表设计(用户信息表,用户包括管理员、教师和学生)

 

 

 

五、运行效果展示

首先在user表中加入管理员信息

 

 

然后运行login.jsp,用username和password做用户名和密码

 

 

点击登录,出现如下界面,再点击菜单

 

 

进入管理员主页

 

 

点击添加教师信息,输入教师信息,点击提交

 

 

 

 看数据库teacher表,教师信息已存入表中

 

 

点击添加学生信息,输入学生信息,点击提交

 

 

  看数据库stu表,学生信息已存入表中

 

 

 

 

 刚才添加的教师信息中教师编号即为教师登录的用户名,密码都默认为123456,刚才添加的学生信息中学生学号即为学生登录的用户名,密码也默认为123456,并且数据已经存入user表中了

 

 

 

接下来,用刚添加的教师编号去登录名叫张三的教师账号

 

点击登录,出现如下界面,再点击菜单

 

 进入教师主界面

 

 

点击添加课程,输入课程信息,点击提交

 

   看数据库cou表,课程信息已存入表中,由于是张三老师登录,所以任课教师为张三,而snum是已选课人数,即当前选择这门课人数为0

 

点击修改个人信息,并填写教师信息,点击提交

 

 

 

 

查看teacher表,可以看到张三教师信息已修改

 

 

 

 

 由于当前没有学生选课,所以浏览学生选课信息为空,我们用刚添加的王五同学账号登录去选课,用户名为王五同学学号,密码默认为123456,输入信息,点击登录

 

 点击菜单,进入学生主界面

 

 

 

 

点击浏览课程信息,可以看见刚才张三教师添加的课程信息

 

 然后点击选课

 

 点击离散数学,可查看课程详细信息,点击选课

 

 

 

 点击返回选课,回到选课最初界面

 

 

 

 此时再点击浏览课程浏览信息,可以看见张三教师的离散数学选课人数由0变为1了

 

这时候,再退出登录,重新登录张三教师账号,点击浏览选课学生信息,可以看见刚才选择张三教师的离散数学的王五同学的信息

 

 

 而登录其他教师账号(比如说教师编号20173506的风一教师账号,点击浏览选课学生信息,则不会看到王五同学的信息)

 

 

继续登录王五同学账号,点击修改个人信息,点击提交

 

 

 

 查看stu表,可以看到王五学生信息已修改

 

 

这个选课系统的运行效果展示结束

 

 

 

以上就是这个选课系统的所有内容,希望与博客园的朋友们共同学习,如果有不足之处,欢迎各位大佬们指正。

posted on 2019-12-25 22:23  yangliuliu  阅读(19223)  评论(63编辑  收藏  举报

导航